ChrisDiRulli
ChrisDiRulli

Reputation: 1512

Locked file or directory in C

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?

Upvotes: 0

Views: 417

Answers (2)

Alexey Orlov
Alexey Orlov

Reputation:

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

Upvotes: 0

Dave
Dave

Reputation: 10577

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)

Upvotes: 5

Related Questions