i want to check a file to see if its been changed and if it is, then load it again.. for this, i started with the following code which is getting me nowhere...
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
using namespace std;
int main()
{
struct stat st;
int ierr = stat ("readme.txt", &st);
if (ierr != 0) {
cout << "error";
}
int date = st.st_mtime;
while(1){
int newdate = st.st_mtime;
usleep(500000);
if (newdate==date){
cout << "same file.. no change" << endl;
}
else if (newdate!=date){
cout << "file changed" << endl;
}
}
}
all the code does is print same file.. no change continuously.
That's because you're calling stat()
outside the loop.
The result from stat() is correct at that particular moment. you need to call stat() again each time you want to check it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With