views:

105

answers:

4

Im programming win32 using fopen fread fwrite in C.

How can I force windows to write data through to the disk? Is than an API call for this?

I have a program that must absolutely make sure that data is saved to disk before it saves to a different indexing file, otherwise if there are crashes the index file can sometimes update but the other file doesn't causing a bad inconsistency.

A: 

See the _commit function. Also at this other MSDN page. This only works with _open and other functions, so see this article - you can link your program with commode.obj to cause fflush to commit to the disk.

wj32
I still giggle whenever I see that object filename.
Crashworks
+2  A: 

Use fflush function after each fwrite

kokeksibir
A: 

Beware that whatever function you use this will not save you against a power outage or hard reset. Hard disks play smart and can report that write has been finished even if they have just put the data into cache and have not actually written it onto surface.

sharptooth
+2  A: 

fflush() does what you want. If you decide to use the Win32 APIs to do your file IO instead of the C STDIO lib, then supply the FILE_FLAG_WRITE_THROUGH flag to CreateFile(), which does exactly what you want.

Crashworks