tags:

views:

148

answers:

2

Hi,

I have a daemon that watches a certain file for changes then processes the changes made to the file.

But now I've noticed that when I attempt to read the file (using fgets) I get no data. This only happens after a change has been made the file, yet if I attempt to read the file at any other time I can read it fine.

I think another process has the file locked when I try to read it. How can I determine if the file is locked?

+4  A: 

When fgets() returns NULL for an EOF, it sets a condition that you need to clear with clearerr() despite the presence of additional data. (The only common type of file locking on Linux is advisory, so that's most likely not your problem. inotify is probably a better solution for detecting file changes. http://en.wikipedia.org/wiki/Inotify)

Dave
It is most unlikely to be file locking, as you say.
Jonathan Leffler
A: 

try checking for error with ferror(), and yes -- as Dave pointed out, you should call clearerr() before fgets to check for in it