views:

113

answers:

4

Hi,

I am currently writing a plugin in C++. For my functionality I ask the API to save out a file. The API gives me a return value when the file is written... or so it seemd. The problem is, that this return value is returned too early so that I can not be sure, that the file is written completely.

Is there a possibility of checking the write completeness of the file independent of the api?

+3  A: 

That's because the system does not write data to disk as soon as it's requested, but still returns. In C, you could use int fflush (FILE *stream), but I don't know how you'd do that in C++.

MeDiCS
+1 of course - this is the answer. Deleted my rubbish one. See this page for C++ i/o http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200109/notes/io.html
ChrisF
Hm, I think I was not clear enough. I have an application that provides me (through a plugin API) the ability to tell the application that it should save the current document. As soon as I do that, I get the return value although it has not finished writing the file. That means the act of saving the file is done by the application. In my opinion this is a bug or misconception in the application. But I have to code around that. I was thinking about checking the file size and see if it stops growing or something like that (although this is pretty scary).I thougt there might be a better way.
draetsch
A: 

Not really, even if we re-read the file, to 'verify' that the write had taken place, you could still be looking at a kernel buffer.

Douglas Leeder
A: 

You could compare the original buffer and the file you have written to byte for byte, but I think it is better to trust your operating system with the according fflush and fclose operations.

Otto Allmendinger
A: 

As mentioned you can use fflush(). you can call sync() / fsync() based upon whether you are using stream class or descriptor.

Adil