Jump to content

I've got a background process up and running. It loads its data from records in a file, and closes the file, and keeps using the loaded records in its main loop. But now I want to add a feature - the process must check the file for any changes, and reload from file if necessary. How will I be able to achieve this feat?

 

Thanks!

Nothing to see here ;)

Link to comment
https://linustechtips.com/topic/618114-check-file-for-changes-periodically-c/
Share on other sites

Link to post
Share on other sites

22 minutes ago, Yamoto42 said:

It's platform dependent, but for each file check the time it was last modified.

For Windows you're looking at GetFileTime(), or for a POSIX system the use the stat() system call..

 

Cool! Thanks a ton!

Nothing to see here ;)

Link to post
Share on other sites

Get file stats , there's API functions for that.

Note however, that some people disable these time updates when they have SSD drives installed on their computers, so just checking the last modified time may not be a universal, commercial solution.

You could also check the file size, if records are added the file size should change, if records may change somewhere in the middle of the file you could read blocks of the file in memory and make CRC or MD5 hashes for these chunks and compare the hashes with the old hashes you already stored somewhere.

 

On Windows 2000 or higher you also have SHChangeNotifyRegister in shell32.dll which monitors the file system (if the file system supports it) and tells you when something changes, see https://msdn.microsoft.com/en-us/library/windows/desktop/bb762120.aspx

There's also FindFirstChangeNotification (windows xp , windows 2003 or higher) which you can use to tell you if something happens inside a folder (file created, closed, changed etc) : https://msdn.microsoft.com/en-us/library/windows/desktop/aa365261(v=vs.85).aspx

On .Net, you have FileSystemWatcher class : https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

Link to post
Share on other sites

7 hours ago, straight_stewie said:

Are you trying to synchronize threads in the same application, or synchronize across two different applications?


No - I'm trying to make the process periodically check the input file for changes, and if it has been modified, open the file, and reload all the records.

 

Nothing to see here ;)

Link to post
Share on other sites

You'll have to either hold that info in memory, or write it to another file that's not being changed, read in the file you want to check, check it against info you have and then if there are changes you would have to open the other file as an ostream file and write to it the changes, or tell you the changes, whatever it is you want to do.

Link to post
Share on other sites

2 minutes ago, jimistephen said:

You'll have to either hold that info in memory, or write it to another file that's not being changed, read in the file you want to check, check it against info you have and then if there are changes you would have to open the other file as an ostream file and write to it the changes, or tell you the changes, whatever it is you want to do.

I am holding the loaded records in memory! What I want to do, is to check if the input file has been modified; If it has, to reopen the file, and reload all the records into memory. It's the checking part that I want to know how...

Nothing to see here ;)

Link to post
Share on other sites

You have to open it and read everything in and just compare it to what's being held in memory. I would create a class with an array (or vector) of what's in memory. Then when you open the file to check you can dynamically make another object of that class and then compare the two classes to each other and if there is any difference you write out the difference on the console( or just say there is a difference). Then if you want to change what's in memory you can just do classObject = tempClassObject and then just delete the temperary one.

Link to post
Share on other sites

Create a class. That class should have a field that is an MD5 checksum of the file, and another field that is a secondary MD5 checksum of the file. The class should have a constructor that sets the primary MD5 field. It should have a method that can be called to generate a checksum of the file. It should also have a method that compares the primary and secondary checksums, and changes the primary if it has been changed, as well as nulling the secondary. The class should have a public method "HasFileChanged" which will automate the methods for you. To accomplish your goal, you should periodically run "HasFileChanged".

 

ENCRYPTION IS NOT A CRIME

Link to post
Share on other sites

51 minutes ago, jimistephen said:

You have to open it and read everything in and just compare it to what's being held in memory. I would create a class with an array (or vector) of what's in memory. Then when you open the file to check you can dynamically make another object of that class and then compare the two classes to each other and if there is any difference you write out the difference on the console( or just say there is a difference). Then if you want to change what's in memory you can just do classObject = tempClassObject and then just delete the temperary one.

WTF? What if it is a massive file, let's say its spanning multiple gigabytes? I got to read the whole file, just to know whether its even changed?

Nothing to see here ;)

Link to post
Share on other sites

11 minutes ago, straight_stewie said:

Create a class. That class should have a field that is an MD5 checksum of the file, and another field that is a secondary MD5 checksum of the file. The class should have a constructor that sets the primary MD5 field. It should have a method that can be called to generate a checksum of the file. It should also have a method that compares the primary and secondary checksums, and changes the primary if it has been changed, as well as nulling the secondary. The class should have a public method "HasFileChanged" which will automate the methods for you. To accomplish your goal, you should periodically run "HasFileChanged".

 

Wow! Great idea! I'll look into it as soon as possible!

 

Thanks!

Nothing to see here ;)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×