1

I want to write a c program that keeps reading a file and whenever a new line is added to that file, its sent across network (lets assume via tcp) to recipient.

What is the best way to do it?

o keep the file open and do something like tail -F on it to keep reading?

o read file on my own?

I am not worried about sending on network part, I need to have best way of getting a new line out of the file. On that line I might do some filtering too before sending.

hari
  • 9,439
  • 27
  • 76
  • 110
  • Is this not a valid question? Or should I frame it differently? Any guidance is appreciated. – hari Jun 17 '11 at 18:08

1 Answers1

2

Linux has inotify, OS X has kqueue/kevent, Windows has similar stuff. All of these let your process block until the kernel notifies you of a change to the file. This is very efficient because your process can sleep until a change actually happens, but obviously these interfaces are not portable.

The only portable approach is to poll the file periodically to see if it changes, but obviously that is not as efficient.

Community
  • 1
  • 1
Nemo
  • 70,042
  • 10
  • 116
  • 153
  • Thanks for the reply. And I agree to you and thats why I am confused. I need to do this operation continuously and it has to be efficient. – hari Jun 16 '11 at 18:11