In C++, what is the proper way to insert a line at the beginning of a text file?

一世执手 提交于 2020-01-19 06:07:25

问题


I am creating a large text file, the first line is a summary which depends on the contents. I am currently inserting a summary line that contains what I expect the summary to be, and after creating the file I compare this with what was actually written. If the information is different I overwrite the initial summary.

Code follows.

  std::ofstream ofile(filename); 
  numOutSegInSynth-=computeSegmentsFromLevel(numLevelsInSynth-1,myInitCkt);

  //extra padding inserted, to prevent summary overwriting segments
  ofile<<numNodesInSynth<<" "<<numOutSegInSynth<<"  0"<<std::endl;
  ofile.close();
  for (long i=0; i<(numLevelsInSynth-1); i++) {  
    //at all
    long numSegOnLevel = computeSegmentsFromLevel(i,myInitCkt); 
    assert(numSegOnLevel); 
    numSegsCreated+=numSegOnLevel;
    buildSegmentsList(i,numSegOnLevel,filename,myInitCkt);   
  }
  if (numSegsCreated != numOutSegInSynth) {
  //update segment count
    std::ofstream ofile(filename, ios::in | ios::out );
    ofile.seekp(0, ios::beg);
    short pred = log10(numOutSegInSynth)+1;
    short act = log10(numSegsCreated)+1;
    if (act>pred) {
      ofile<<numNodesInSynth<<" "<<numSegsCreated<<" 0"<<std::endl;
    } else {
      ofile<<numNodesInSynth<<" "<<numSegsCreated<<"  0"<<std::endl;
    }
      ofile.close();
    }

I would like to know if it is possible to just append to the start of the file, either to insert an new line or a single character Thanks


回答1:


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.

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> _

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.

Cheers & hth.,




回答2:


Most filesystems are designed to only have data added at the end. Inserting data at the beginning requires rewriting the file.




回答3:


The short answer is you cannot just prepend the summary to the beginning of the file once data has been written to it. But you still can achieve the desired effect. Just don't write all the data to the file at one. If you can afford it, keep it in some buffer area, like a string stream. Then, when time comes to write the file, first write the summary, then dump the contents of the buffer into the file. Another way would be to create a new file, write the summary there, then append the contents of the original file.



来源:https://stackoverflow.com/questions/4179349/in-c-what-is-the-proper-way-to-insert-a-line-at-the-beginning-of-a-text-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!