gpt4 book ai didi

c - C中的Inotify事件

转载 作者:太空狗 更新时间:2023-10-29 15:32:07 25 4
gpt4 key购买 nike

程序:

#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 修改了文件,如何获取文件名。

最佳答案

文档说:

The name field is present only when an event is returned for a file inside a watched directory; it identifies the file pathname relative to the watched directory. This pathname is null-terminated, and may include further null bytes ('\0') to align subsequent reads to a suitable address boundary.

所以您的问题是您期望 inotify 将名称“回显”给您,但这不是它的工作方式。

关于c - C中的Inotify事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34418418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com