LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Getting errors with inotify (https://www.linuxquestions.org/questions/linux-newbie-8/getting-errors-with-inotify-4175500997/)

MichaelStein 04-08-2014 11:12 AM

Getting errors with inotify
 
Hi there fellas,

I am new to inotify. I was trying to copy an example from a web tutorial but when I try to run it I get errors. To be more specific, I am trying to find out why the function 'read()' is being recorded as an error.

//This is the sample program to notify us for the file creation and file deletion takes place in “/tmp” directory
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/inotify.h>
//#include <linux/inotify.h>

#define EVENT_SIZE ( sizeof( struct inotify_event) )
#define EVENT_BUF_LEN ( 1024*( EVENT_SIZE) )

int main()
{
int length, i =0;
int fd;
int wd;
char buffer( EVENT_BUF_LEN);

fd = inotify_init();

if( fd < 0)
{
perror( "inotify_init" );
}

wd = inotify_add_watch( fd, "/home/michaelstein/Desktop/Data.txt", IN_CREATE | IN_DELETE );

length = read(fd, buffer, EVENT_BUF_LEN);

if( length < 0 )
{
perror( "read" );
}

while( i < 0 )
{
struct inotify_event *event = ( struct inotify_event * ) &buffer[i];
if( event->length )
{
if(event->mask & IN_CREATE )
{
if(event->mask & IN_ISDIR )
{
printf("New directory %n was created.\n", event->name);
}
else
{
printf("New file %n was created. \n", event->name);
}
}
else if(event->mask & IN_DELETE)
{
if(event->mask & IN_ISDIR )
{
printf("Directory %n deleted.\n", event->name);
}
else
{
printf("File %n deleted. \n", event->name);
}
}
}
i += EVENT_SIZE + event->len;
}

inotify_rm_watch( fd, wd );

close( fd );

}

I the following errors:

inotify.cxx: In function ‘int main()’:
inotify.cxx:16:28: warning: overflow in implicit constant conversion [-Woverflow]
inotify.cxx:27:41: error: ‘read’ was not declared in this scope
inotify.cxx:36:69: error: invalid types ‘char[int]’ for array subscript
inotify.cxx:37:14: error: ‘struct inotify_event’ has no member named ‘length’

If anyone has anything that could enlighten me I would be very grateful. Additionally, if anyone could point to a code snippet about inotify or else a site that gives detailed information about inotify I would be equally indebted.
Thank you.

smallpond 04-08-2014 03:35 PM

Because you aren't including the header file that read calls out:

Code:

man 2 read

NAME
      read - read from a file descriptor

SYNOPSIS
      #include <unistd.h>

      ssize_t read(int fd, void *buf, size_t count);



All times are GMT -5. The time now is 10:27 AM.