Risky101
asked on
How to flush a file to disc to guard against data loss
Hi,
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
of.close();
Best regards,
Shane.
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
of.close();
Best regards,
Shane.
ASKER
The problem only occurs when the computer is switched off at the wall.
I tried of.fflush(), but it still doesnt work.
I tried of.fflush(), but it still doesnt work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It wouldn't be too hard to recode it - I will try that.
Also, function calls like fsync() would work with a file handle too. Thanks, I'll let you know how it goes.
Also, function calls like fsync() would work with a file handle too. Thanks, I'll let you know how it goes.
ASKER
Thanks - this worked.
you might want to put it somewhere in your shutdown scripts.