Good day all,
I have been working on a program that monitors(during runtime) for when a file is accessed by the user.
Here is the code:
void LogFiles::Write(string filename)
struct stat attrib; //Creating a file attribute structure
stat(filename.c_str(), &attrib); //get the attributes of filename.txt
time_t lasttime = attrib.st_atime;
time_t newtime = lasttime;
/*Compare the previous time the file was accessed to the current time it was accessed. Loops until they differ. If they differ, exit the loop and record to the console. Note: the times will only differ if the user accesses the file during runtime.*/
while( newtime == lasttime )
newtime = attrib.st_atime;
cout << "User accessed file " << filename " at: " << ctime(&(newtime)) << endl;
The code ALMOST works. However, upon running the program, I noticed that it only records to the console if I access AND MODIFY the file, and then access the file again. Otherwise, it seems never to exit the while loop.
I have given it some thought and I realised that the "newtime" variable/value only changes if the file is modified and then accessed.
1. Does anyone know why this is?
2. Does anyone have a suggestion how I can fix this?