Reputation: 3
I have a set of configuration files (10 or more), and if user opens any of these file using any editor (e.g vim,vi,geany,qt,leafpad..). How would I come to know that which file is opened and if some writing process is done, then it is saved or not (using C code).
Upvotes: 1
Views: 86
Reputation: 5870
For the 1st part of your question, please refer e.g. to How to check if a file has been opened by another application in C++?
One way described there is to use a system tool like lsof and call this via a system()
call.
For the 2nd part, about knowing whether a file has been modified, you will have to create a backup file to check against. Most editors already do that, but their naming scheme is different, so you might want to take care of that yourself. How to do that? Just automatically create a (hidden) file .mylogfile.txt
if it does not exist by simply copying mylogfile.txt
. If .mylogfile.txt
exists, is having an older timestamp than mylogfile.txt
, and differs in size and/or hash-value (using e.g. md5sum
) your file was modified.
But before re-implementing this, take a look at How do I make my program watch for file modification in C++?
Upvotes: 1