Solved

Problems with file buffers. Flushing synchronously

Posted on 2004-04-15
4
320 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why should double free() crash? 7 32
script 11 137
Trying to compile syslinux 4.03 on Ubuntu 14.04 14 144
null coming in rpm name 3 61
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now