Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-08-02
5
Medium Priority
?
217 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

609 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