tags:

views:

94

answers:

4

Can I use C to know whether a file operation is performed (and where it is performed) on the disk at run time?

+1  A: 

Check out the following sample from MSDN: Change Notify Watcher Sample

Aviad P.
+1  A: 

You need to rely on specific OS APIs, which you can use in the C programming language.

Gregory Pakosz
+1  A: 

As others have said here, there is no platform independent / C standard way.

On Linux you can uses the inotify api. There is a tutorial on IBM developerworks.

dmeister
A: 

The answer to your question depends on how you meant it. If you're asking if you can detect a physical operation on the disk (seek, read, or write), the answer is no. Disks have disk caches to hide these operations from you in the name of performance. You can tell when the cache is changed using the mechanisms above, but not when the physical operation is performed.

This is why we have journaling filesystems. Even with journaling, however, there is the possibility of actual data loss if there is a catastrophic failure and the cache cannot be saved.

Of the above methods, I have used the FileSystemWatcher, and it works very well. You can select the events you're interested in being notified about the your program will receive an program event when the file event takes place.

Note that, at least under Windows/C#, the event comes in on a separate thread, so you'll have to use delegates to signal the main (or whatever) thread that the event has taken place.

jfawcett