Created
February 28, 2014 05:25
-
-
Save matutter/9265816 to your computer and use it in GitHub Desktop.
The basics of detecting a file change event without excess libraries. Very inefficient.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstring> | |
#define _missingFile 0x601DE0 | |
using namespace std; | |
class fileListener | |
{ | |
private: | |
struct stat st; | |
public: | |
class file_ | |
{ | |
public: | |
string name; | |
unsigned long long modify; | |
unsigned int mode; | |
}; | |
file_ file; | |
int addListener(string s) { | |
file.name = s; | |
stat( file.name.c_str(), &st ); | |
file.modify = st.st_mtime; | |
file.mode = st.st_mode; | |
return ( file.mode != _missingFile )? 1 : 0; | |
} | |
int listenModify(void) { | |
while(1) | |
{ | |
stat( file.name.c_str(), &st ); | |
file.mode = st.st_mode; | |
if( file.mode == _missingFile ) return 0; | |
if( file.modify != st.st_mtime ) | |
{ | |
file.modify = st.st_mtime; | |
return 1; | |
} | |
} | |
return 0; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fileListener fl.addListender("filename.txt");
while(fl.listenModify) { cout << "file changed!"; }