且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C ++中,在文本文件开头插入行的正确方法是什么?

更新时间:2023-11-15 09:06:52

正如Ignacio说,大多数文件系统不支持在文件中插入,更不用说文本文件。而C ++标准库的一个文件的概念是一个简单的字节流。因此,在开始时,一个固定大小的摘要,因为它似乎是你的意图与你的代码,是一个实用的解决方案(注意:你的代码似乎没有达到这个固定的大小)。

As Ignacio says, most filesystems don't support insertion in files, not to mention text files. And the C++ standard library's notion of a file is a simple stream of bytes. And so a fixed size summary at the start, as it seems you were intending with your code, is one practical solution (note: your code doesn't appear to achieve this fixed size).

但是,一些常见的文件系统确实支持将额外的信息与文件关联。

However, some common filesystems do support associating extra information with files.

例如,Windows NTFS支持多个数据流文件:

For example, Windows NTFS supports multiple data streams per file:


C:\test> echo blah blah >data.txt

C:\test> type data.txt
blah blah

C:\test> echo some info >data.txt:summary

C:\test> type data.txt
blah blah

C:\test> more <data.txt:summary
some info

C:\test> _

例如,这是Windows资源管理器用于向文件添加摘要信息的机制。

E.g., this is the mechanism used by Windows Explorer for adding summary info to files.

所以,如果你为一个支持多个流的系统编程,并且不计划将程序或数据移植到其他系统,你可以考虑一个多流文件。

So, if you're programming for a system that supports multiple streams, and don't plan on porting the program or the data to other systems, you might consider a multiple-stream file.

第三种方法是通过拥有两个或多个关联文件来有效实现自己。例如。一个数据文件和一个摘要文件。使用例如一个连接它们的命名约定。

A third alternative is to effectively implement that yourself, by having two or more associated files. E.g. one data file, and one summary file. With e.g. a naming convention connecting them.

hth。,