Syncing independent applications. (How to check if a file was modified by another program on runtime)

StackOverflow https://stackoverflow.com/questions/18849120

Question

It is easier to explain with example.

When 2 text editors edit the same text file in the same time, when one editor saves the file, the other one understands that it was modified and asks to do smth.

How is it possible to get a signal that a file was modified outside the program?

I am working with c++ (though I think it isn't important) and on linux. (solution for windows would be good too)

Était-ce utile?

La solution

ISO-C++ does not offer this functionality, so you have to stick with what the operating system provides.

On Linux that would be inotify, on Windows you would use directory change notifications.

Autres conseils

① Check the timestamp of the file as close as possible before writing. If it is not what it was when you last opened this file for reading, then beware!

② You can build a checksum of the file and compare this to one you built earlier.

③ Register to a system service which informs you about file activities. This depends on the goodwill of the OS you are using; if this notification service isn't working properly, your stuff will fail. On Linux have a look at Inotify.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top