0

Suppose I open() a file on a *nix machine and I hit eof. But when I open()ed it, it was still being written.

Is there a way to detect if the file has grown since open()-time? Or do I need to close() and re-open() to check?

Is there a way to read past the old eof without close()ing and reopen()ing?

Fixee
  • 1,581
  • 2
  • 15
  • 25
  • What about checking the mtime and see if that's later than when you opened the file? See http://stackoverflow.com/questions/4021479/getting-file-modification-time-on-unix-using-utime-in-c – lreeder Sep 18 '13 at 21:14
  • And, possibly a dup of http://stackoverflow.com/questions/1087204/how-can-i-tell-if-a-file-is-open-elsewhere-in-c-on-linux – lreeder Sep 18 '13 at 21:20

1 Answers1

1

Although not portable, on linux you can use the inotify system and watch for file close events for your file of interest. See http://www.ibm.com/developerworks/library/l-inotify/.

Note that just because the file was closed by the other process doesn't necessarily mean its size has increased. If you care about file size changes, use stat() to check for size changes before you close it. See How do you determine the size of a file in C?

Community
  • 1
  • 1
lreeder
  • 12,047
  • 2
  • 56
  • 65