Note that there are some explanatory texts on larger screens.

plurals
  1. POprogram that reacts to inotify and prints the events
    primarykey
    data
    text
    <p>I am working in Ubuntu. I want to monitor a folder and print every event that pops up in the subfolders (print files). I have the following code but it doesn't work. When executed, there is no println of the events.</p> <p>In the second code I only see the events from the folder. The events from each subfolder do not pop up.</p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; #include &lt;stdio.h&gt; using namespace std; std::string exec(char* cmd) { FILE* pipe = popen(cmd, "r"); if (!pipe) return "ERROR"; char buffer[256]; std::string result = ""; while(!feof(pipe)) { if(fgets(buffer, 256, pipe) != NULL) result += buffer; } pclose(pipe); cout&lt;&lt;"result is: "&lt;&lt;result&lt;&lt;endl; return result; } int main() { //while(1) //{ string s=exec((char*)"inotifywait -rme create /home/folder/"); cout &lt;&lt; s &lt;&lt; endl; //} return 0; } </code></pre> <p>This code only prints the events from the folder I'm monitoring. It doesn't print the events from each subfolder. I don't know how to improve it for my needs.</p> <pre><code>#include &lt;sys/inotify.h&gt; #include &lt;sys/ioctl.h&gt; #include &lt;iostream&gt; void processNewFiles(int fd, int wd); int main(int argc, char** argv) { const char* dirPath = "/home/folder/" ;//argv[1]; int fd = inotify_init(); int wd = inotify_add_watch(fd, dirPath, IN_CREATE); if (wd) { processNewFiles(fd, wd); inotify_rm_watch(fd, wd); } } void processNewFiles(int fd, int wd) { bool done = false; do { int qLen = 0; ioctl(fd, FIONREAD, &amp;qLen); char* buf = new char[qLen]; int num = read(fd, buf, qLen); if (num == qLen) { inotify_event* iev = reinterpret_cast&lt;inotify_event*&gt;(buf); if (iev-&gt;wd == wd &amp;&amp; iev-&gt;mask &amp; IN_CREATE) { std::cout &lt;&lt; "New file created: " &lt;&lt; iev-&gt;name &lt;&lt; std::endl; } } delete [] buf; } while (!done); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload