title: In C++, what is the proper way to insert a line at the beginning of a text file?
body: <p>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.</p>
  
  <p>Code follows.</p>
  
  <pre><code>  std::ofstream ofile(filename); 
    numOutSegInSynth-=computeSegmentsFromLevel(numLevelsInSynth-1,myInitCkt);
  
    //extra padding inserted, to prevent summary overwriting segments
    ofile&lt;&lt;numNodesInSynth&lt;&lt;" "&lt;&lt;numOutSegInSynth&lt;&lt;"  0"&lt;&lt;std::endl;
    ofile.close();
    for (long i=0; i&lt;(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&gt;pred) {
        ofile&lt;&lt;numNodesInSynth&lt;&lt;" "&lt;&lt;numSegsCreated&lt;&lt;" 0"&lt;&lt;std::endl;
      } else {
        ofile&lt;&lt;numNodesInSynth&lt;&lt;" "&lt;&lt;numSegsCreated&lt;&lt;"  0"&lt;&lt;std::endl;
      }
        ofile.close();
      }
  </code></pre>
  
  <p>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 </p>