Solved

How to flush a file to disc to guard against data loss

Posted on 2004-08-02
5
212 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:Risky101
  • 3
5 Comments
 
LVL 3

Expert Comment

by:pYrania
ID: 11701104
$ sync
you might want to put it somewhere in your shutdown scripts.
0
 

Author Comment

by:Risky101
ID: 11707325
The problem only occurs when the computer is switched off at the wall.

I tried of.fflush(), but it still doesnt work.
0
 
LVL 6

Accepted Solution

by:
de2Zotjes earned 175 total points
ID: 11708584
I see you are using c++ so my advice is a bit of, but I imagine that there is an ios equivalent for it.

The open call accepts a flag O_DIRECT to minimize the effects of buffering. It is bad for io-performance (for obvious reasons) but if the amounts of data are small it is quite acceptable

Hmm.. just had a quick look around the net.. maybe there is no O_DIRECT equivalent in ios::stream. Would also make sense, it is a stream interface, buffers will be part of that.

Damn, you might have to recode this bit to talk to a file directly instead of a stream...
0
 

Author Comment

by:Risky101
ID: 11709639
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.
0
 

Author Comment

by:Risky101
ID: 11754156
Thanks - this worked.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Trying to install MonoDevelop from sourde on Ubuntu 14.04 trusty 10 595
Debian 3 - initrd rebuild 3 78
Chinese translation in RedHat 10 55
Shell script for mvn build 2 76
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question