Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to listen to the change in dir or file?
    text
    copied!<p>I have code to listen change in directory, When I run it writes nothing in the file neither shows in out out on terminal. </p> <p>Can any one please help me out to listen the changes take place in particular text file? I want to store timestamp and what change took place. </p> <p>Code is:</p> <pre><code>/*This is the sample program to notify us for the file creation and file deletion takes place in “/tmp” directory*/ #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;errno.h&gt; #include &lt;sys/types.h&gt; #include &lt;linux/inotify.h&gt; #define EVENT_SIZE ( sizeof (struct inotify_event) ) #define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) ) int main( ) { int length, i = 0; int fd; int wd; char buffer[EVENT_BUF_LEN]; char str[] = "This is tutorialspoint.com"; FILE* fp=NULL;; fp=fopen("f1.txt","rw+"); /*creating the INOTIFY instance*/ fd = inotify_init(); /*checking for error*/ if ( fd &lt; 0 ) { perror( "inotify_init" ); } /*adding the “/tmp” directory into watch list. Here, the suggestion is to validate the existence of the directory before adding into monitoring list.*/ wd = inotify_add_watch( fd, "/tmp", IN_CREATE | IN_DELETE ); /*read to determine the event change happens on “/tmp” directory. Actually this read blocks until the change event occurs*/ length = read(fd, buffer, EVENT_BUF_LEN ); /*checking for error*/ if ( length &lt; 0 ) { perror( "read" ); } /*actually read return the list of change events happens. Here, read the change event one by one and process it accordingly.*/ while ( i &lt; length ) { struct inotify_event *event = ( struct inotify_event * ) &amp;buffer[ i ]; if ( event-&gt;len ) { if ( event-&gt;mask &amp; IN_CREATE ) { if ( event-&gt;mask &amp; IN_ISDIR ) { printf( "New directory %s created.1\n", event-&gt;name ); fwrite(str , 1 , sizeof(str) , fd ); } else { printf( "New file %s created.\n2", event-&gt;name ); fwrite(str , 1 , sizeof(str) , fd ); } } else if ( event-&gt;mask &amp; IN_DELETE ) { if ( event-&gt;mask &amp; IN_ISDIR ) { fwrite(str , 1 , sizeof(str) , fd ); printf( "Directory %s deleted.\n", event-&gt;name ); } else { printf( "File %s deleted.\n", event-&gt;name ); } } } i += EVENT_SIZE + event-&gt;len; } /*removing the “/tmp” directory from the watch list.*/ inotify_rm_watch( fd, wd ); fclose(fd); /*closing the INOTIFY instance*/ close( fd ); } </code></pre>
 

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