Reputation: 24825
The following code
#include <fstream>
void print( std::ofstream &f, int a ) {
f << a << '\n';
}
int main () {
std::ofstream fout( "out.txt" );
print( fout, 1 );
print( fout, 2 );
return 0;
}
produces an output like this
1
2
However I want to see only 2. In other words, I want to overwrite the content of the output file whenever I call the print.
The reason for that, is that I want to call an update function in intervals. As a result, each time the update function is called, the new stats should appear in the output file (not appending the current with previous one).
P.S: putting fout.clear()
between the two print calls won't do the job.
Upvotes: 0
Views: 137
Reputation: 1
Simply use the std::ofstream::seekp()
to reset the output position between the print()
calls
std::ofstream fout( "out.txt" );
print( fout, 1 );
fout.seekp(0); // <<<<
print( fout, 2 );
Note that fout.clear()
just resets the error states of the stream.
Upvotes: 2