Solved

Problems with file buffers. Flushing synchronously

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
File system unmounted 3 157
Percona cluster member randomly crash 15 534
Avoiding pop-up when starting a linux scrip/shell 5 109
Trying to compile syslinux 4.03 on Ubuntu 14.04 14 175
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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