Malte Dreschert
Malte Dreschert

Reputation: 71

Check if writing to a file is finished

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?

Upvotes: 2

Views: 1782

Answers (4)

Adil
Adil

Reputation: 2538

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

Upvotes: 0

Otto Allmendinger
Otto Allmendinger

Reputation: 28278

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.

Upvotes: 0

Douglas Leeder
Douglas Leeder

Reputation: 53320

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.

Upvotes: 1

moatPylon
moatPylon

Reputation: 2191

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++.

Upvotes: 4

Related Questions