Skip to content

Instantly share code, notes, and snippets.

@matutter
Created February 28, 2014 05:25
Show Gist options
  • Save matutter/9265816 to your computer and use it in GitHub Desktop.
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.
#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;
}
};
@matutter
Copy link
Author

fileListener fl.addListender("filename.txt");
while(fl.listenModify) { cout << "file changed!"; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment