aboutsummaryrefslogtreecommitdiff
path: root/experiments/watch_file_changes.c
diff options
context:
space:
mode:
Diffstat (limited to 'experiments/watch_file_changes.c')
-rw-r--r--experiments/watch_file_changes.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/experiments/watch_file_changes.c b/experiments/watch_file_changes.c
new file mode 100644
index 0000000..04fd3b2
--- /dev/null
+++ b/experiments/watch_file_changes.c
@@ -0,0 +1,30 @@
1#include <stdio.h>
2#include <unistd.h>
3#include <sys/stat.h>
4#include <time.h>
5
6int main(void) {
7 struct stat file_stat;
8 time_t last_mod_time;
9 const char* file = "watch_file_changes.txt";
10
11 stat(file, &file_stat);
12 last_mod_time = file_stat.st_mtime;
13
14 while (1) {
15 sleep(1);
16 stat(file, &file_stat);
17 if (file_stat.st_mtime != last_mod_time) {
18 last_mod_time = file_stat.st_mtime;
19
20 struct tm* time_info = localtime(&last_mod_time);
21 char formatted_time[9];
22 strftime(formatted_time, sizeof(formatted_time), "%H:%M:%S", time_info);
23
24 printf("File %s changed at %s\n", file, formatted_time);
25 }
26 }
27 printf("hi\n");
28 return 0;
29}
30