How to flush a file to disc to guard against data loss
Posted on 2004-08-02
I've got a counter in a file that is regularly updated. Unfortunately, if the system is powered off, the counter is a few behind what it should be, and data is getting overwritten when the system starts up again.
What is the linux call to flush the file to disc, for the code below? I've tried fflush(), but it doesnt work with ofstream.
string countFileName = cfg.getDataDirectory() + "NOISE_COUNTER";
ofstream of(countFileName.c_str(), ios::out);
of << ++vm.imgCount_;
#warning THIS FILE IS NOT BEEN FLUSHED TO DISC - ON POWER FAILURE THE COUNT GOES BACK A FEW STEPS