Solved

Problems with file buffers. Flushing synchronously

Posted on 2004-04-15
4
348 Views
Last Modified: 2010-04-21
I am trying to write a file to and then I want to ensure that all the data is written to it. I was going to the the flush command, but snooping on a serial port I can see that the file buffer is not fully flushing the file.

I'm converting from Windows to Linux and in Win I see that it uses flushfilebuffers on a drive letter so I was going to do the same on the drive relating to the device : /dev/sdf

In the fstab file I have configured my drive to be synchronous :

/dev/sdf     /media/mydisk        auto       noauto,users,exec,sync      0 0

Now this seems to help but the final portions of the file are not copied and because of this my program cannot complete the updating command.

I am using QFile, this is my code:

QFile myFile("/media/myDisk/myfile");
myFile.flush();
myFile.close();

I have also tried flushing the actual device /media/myDisk, but this does not make any difference either.

Atm I am getting round the problem by going to sleep for a while until the delayed write finishes but this is not really much of a solution.

Any ideas?

regards,

Gordon
0
Comment
Question by:jeffreyg
[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
  • 2
4 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10831781
Hi jeffreyg,

If you want to eliminate buffering alltogther, use setvbuf() towards the beginning of the program ... I suspect Qt uses stdio functions so this should do the trick

Another option will be to use read and write for file operations as these calls are not buffered

Sunnycoder
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10832786
Hi jeffreyg,

If you use buffered I/O, you need a two-stage sync:

fflush(your_FILE);         // Flush your buffers to system
fsync(fileno(your_FILE));  // Flush system's write cache to disk



Cheers,
Stefan
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 250 total points
ID: 10834622
jeffreyg,
For a QFile, the flush to disk is slightly different:

QFile myFile;
myFile.flush();
fsync(myFile.handle());

Stefan
0
 

Author Comment

by:jeffreyg
ID: 11013108
Stefan73,

Thanks for the input. Got it working now. Used your fsync code and implemented it in the following way:

QFile myFile;
myFile.flush();
fsync(myFile.handle());
myFile.close();

Thanks, points r yours!
Thanks also to sunnycoder for your time.

regards,

G.
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

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 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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

705 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