• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

Problems with file buffers. Flushing synchronously

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
jeffreyg
Asked:
jeffreyg
  • 2
1 Solution
 
sunnycoderCommented:
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
 
stefan73Commented:
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
 
stefan73Commented:
jeffreyg,
For a QFile, the flush to disk is slightly different:

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

Stefan
0
 
jeffreygAuthor Commented:
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now