Solved

Problems with file buffers. Flushing synchronously

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
script 11 136
what is Amazon Linux AMI 6 53
Automating a set of commands run on linux 7 94
Linux Search 3 64
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

21 Experts available now in Live!

Get 1:1 Help Now