Friday, 15 June 2012

c++ - Overwritting the content of file on each std::ofstream call -



c++ - Overwritting the content of file on each std::ofstream call -

the next code

#include <fstream> void print( std::ofstream &f, int ) { f << << '\n'; } int main () { std::ofstream fout( "out.txt" ); print( fout, 1 ); print( fout, 2 ); homecoming 0; }

produces output this

1 2

however want see 2. in other words, want overwrite content of output file whenever phone call print.

the reason that, want phone call update function in intervals. result, each time update function called, new stats should appear in output file (not appending current previous one).

p.s: putting fout.clear() between 2 print calls won't job.

simply utilize std::ofstream::seekp() reset output position between print() calls

std::ofstream fout( "out.txt" ); print( fout, 1 ); fout.seekp(0); // <<<< print( fout, 2 );

note fout.clear() resets error states of stream.

c++ file fstream

No comments:

Post a Comment