?
Solved

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

Posted on 2004-08-02
5
Medium Priority
?
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 700 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

DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

Question has a verified solution.

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

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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

743 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