Solved

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

Posted on 2004-08-02
5
215 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 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
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 …

707 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