LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-08-2014, 12:12 PM   #1
MichaelStein
LQ Newbie
 
Registered: Apr 2014
Posts: 14

Rep: Reputation: Disabled
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.
 
Old 04-08-2014, 04:35 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,609

Rep: Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702
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);
 
  


Reply

Tags
inotify


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using inotify sniper8752 Linux - Newbie 1 04-06-2013 09:37 PM
[SOLVED] inotify Navodar Linux - Kernel 1 02-08-2011 01:03 PM
inotify chymeira Linux - General 3 03-06-2009 05:58 AM
udev and inotify aeruzcar Linux - General 3 03-24-2007 01:42 PM
Mounting Inotify? trscookie Linux - Software 1 12-19-2005 02:37 PM


All times are GMT -5. The time now is 08:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration