Reputation: 176
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
Upvotes: 2
Views: 5503
Reputation: 145204
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.,
Upvotes: 3
Reputation:
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.
Upvotes: 2
Reputation: 798526
Most filesystems are designed to only have data added at the end. Inserting data at the beginning requires rewriting the file.
Upvotes: 3