Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在运行系统时解决问题_C++_C_Inotifypropertychanged - Fatal编程技术网

C++ 在运行系统时解决问题

C++ 在运行系统时解决问题,c++,c,inotifypropertychanged,C++,C,Inotifypropertychanged,我想监视一个文件夹 每次弹出通知时,我都要运行一个系统命令行。 使用系统命令时出现问题。每个新事件会弹出3次,尽管它应该会弹出一次。 编辑: Thx为您播放。我发现了窃听器。系统执行了一个位于受监视文件夹内的文件夹。这就是为什么每次我在监控文件夹中丢弃一个foder时,事件会被打印3次 代码----------- 没有在里面睡觉,而(代码发布)我有: 您正在if条件下运行system(),每次看到打开的文件时都会调用该条件 因此,每当您打开一个文件时,它将无限运行。你必须想办法摆脱这种循环 当执

我想监视一个文件夹

每次弹出通知时,我都要运行一个系统命令行。 使用系统命令时出现问题。每个新事件会弹出3次,尽管它应该会弹出一次。 编辑: Thx为您播放。我发现了窃听器。系统执行了一个位于受监视文件夹内的文件夹。这就是为什么每次我在监控文件夹中丢弃一个foder时,事件会被打印3次

代码-----------

没有在里面睡觉,而(代码发布)我有:


您正在if条件下运行
system()
,每次看到打开的文件时都会调用该条件

因此,每当您打开一个文件时,它将无限运行。你必须想办法摆脱这种循环

当执行到达换行符时,它从内部while中断,但是外部while(1)呢

根据要求(工作代码):

#包括
#包括
#包括
#包括
int main(){
int-fd,wd,wd1,i=0,len=0;
字符路径名[100],buf[1024];
结构inotify_事件*事件;
fd=inotify_init1(在非块中);
wd=inotify\U add\U watch(fd,“/tmp/temp”,在所有事件中);
而(1){
//将1024字节的事件从fd读入buf
i=0;
len=读取(fd,buf,1024);
while(imask&IN_创建){
系统(“/bin/date”);
}
i+=sizeof(struct inotify_event)+event->len;
}  
}
}

上述代码在每次将文件添加到/tmp/temp时执行$date命令。修改它以满足您的需要。

您的系统命令在做什么?是打开你要更改的文件吗?如果是这样的话,这难道不会导致代码再次运行并将您放入无限循环(这显然是您看到的)

编辑-有一个网站正在做与您非常相似的事情。我注意到他们使用了一个更大的缓冲区,注释中提到了一些关于结构对齐的内容,所以是否有可能您得到了一个错误的事件长度,这会导致缓冲区溢出,并迫使您的代码循环超过预期的时间


这样,我会检查(使用一点printf调试逻辑)是否按预期循环。

第一个if(){}块中有一个中断。我不会在这个块之后递增,它将跳过其余已读内容。提示:使用for(;;)循环,这更清晰。我添加了break,因为我需要。但事实并非如此working@justAngela但是,外部while(1)循环呢。这将导致再次执行内部循环。请看我的答案。Break只会从内部循环中中断(这毫无意义)。您正在使用system()从另一个程序调用此程序?inotify应该监视文件夹,只要您愿意。当我将文件放入受监视的文件夹中时,我想执行另一个程序。执行可执行文件时出现问题。它无限次地执行它,但我不想这样。我只想在我将文件放入monitord文件夹时执行它,但我不想退出循环。我正在使用inotify..它监视一个floder是否有滴水。因此,尽管如此needed@justAngela这是你的代码的精简版本。两个while循环。if条件是使用system()call.thx进行重播的部分(在in_OPEN中)。如果我想在每次将文件放入monitor文件夹时运行系统,该如何操作/上述代码肯定只会在第一次打开某个文件时执行?因此,如果OP插入另一个文件,他们将看不到第二个事件?thx用于重播。我需要在每次将文件放入受监视的文件夹时执行可执行文件。怎么做?似乎我的代码工作不正常
    /*

    Simple example for inotify in Linux.

    */


    #include <sys/inotify.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    int main(){
        int fd,wd,wd1,i=0,len=0;
        char pathname[100],buf[1024];
        struct inotify_event *event;

        fd=inotify_init1(IN_NONBLOCK);
        /* watch /test directory for any activity and report it back to me */

 `wd=inotify_add_watch(fd,"/home/folder",IN_ALL_EVENTS`);

        while(1){
            //read 1024  bytes of events from fd into buf
            i=0;
            len=read(fd,buf,1024);
            while(i<len){
                event=(struct inotify_event *) &buf[i];


                /* check for changes */
                if(event->mask & IN_OPEN)
                 {  // printf("\n %s :was opened\n",event->name);
                    char*path="/home/folder/";
                    char*file=event->name;
                    int n=sizeof(path)+sizeof(file);
                    char *result=(char *)malloc(512);
                    strcpy(result,path); // copy string one into the result.
                    strcat(result,file); // append string two to the result
                    puts (result);

                    //printf("RESUULT:");

                    int pp=sizeof(result);
                    char *run="/home/test/./userr ";
                    int l=sizeof(run);

                    char *cmd=(char *)malloc(1000);
                    strcpy(cmd,run);
                    strcat(cmd,result);
                    puts (cmd);

                    system(cmd);
                    printf("\n %s :was opened\n",event->name);
                    //break;

            }
                if(event->mask & IN_MODIFY)
                      printf("%s : modified\n",event->name);

                if(event->mask & IN_ATTRIB)
                      printf("%s :meta data changed\n",event->name);

                if(event->mask & IN_ACCESS)
                      printf("%s :was read\n",event->name);

                if(event->mask & IN_CLOSE_WRITE)
                      printf("%s :file opened for writing was closed\n",event->name);

               // if(event->mask & IN_DELETE)
                //    printf("%s :deleted\n",event->name);

                /* update index to start of next event */
                i+=sizeof(struct inotify_event)+event->len;
            }

        }

    }
 :was opened
/home/folder/
   :file opened not for writing was closed
 1002_copy :was opened
/home/folder/1002_copy
1002_copy :file opened not for writing was closed
#include <sys/inotify.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(){
    int fd,wd,wd1,i=0,len=0;
    char pathname[100],buf[1024];
    struct inotify_event *event;

    fd=inotify_init1(IN_NONBLOCK);
    wd=inotify_add_watch(fd,"/tmp/temp",IN_ALL_EVENTS);
    while(1){
        //read 1024  bytes of events from fd into buf
        i=0;
        len=read(fd,buf,1024);
        while(i<len){
            event=(struct inotify_event *) &buf[i];
            /* check for changes */
            if(event->mask & IN_CREATE){
                system("/bin/date");
            }
            i+=sizeof(struct inotify_event)+event->len;
        }  
    }
}