【发布时间】:2015-12-22 14:45:22
【问题描述】:
程序:
#include <stdio.h>
#include <sys/inotify.h>
int main()
{
int fd = inotify_init();
int wd1 = inotify_add_watch(fd, "/home/guest/a", IN_MODIFY);
struct inotify_event *event = (struct inotify_event*) malloc(sizeof(struct inotify_event));
read(fd, event, 1000);
if (event->mask & IN_MODIFY) {
printf("File '%s' is modified\n", event->name);
}
}
输出:
$ ./a.out
File '' is modified
$
如果文件 a 被修改,我希望上面的程序会通知文件名。但它通知没有文件名。所以, 如果使用 inotify 修改了文件,如何获取文件名。
【问题讨论】:
-
Do not cast the return value of
malloc(), andvoid *in c in general。你真的不需要malloc()任何东西。只需struct inotify_event event; read(fd, &event, sizeof(event))。会做 -
查看
read()的返回值。