What is the fastest method in C++, to read a new line from a file which is written by another process. Or how my program can 开发者_如何学Pythonbe notified that there is a new line in file so read it? (in linux)
The fastest method is to use pipes or events (for Windows apps).
If you still want use files, first of all that you really need, making sure, that a file has been really modified (use seek and compare it with prew value). Than go to the 'last val of seek' and read it.
And it will be better use mutex (if you read data from file).
Assuming the OS supports concurrent file access, all you should need to do is seek to EOF, wait for the stat to change then try to read from the file. You might want to add in a sleep to slow down the loop.
The 'tail' command on POISX (with the -f option) implements this - source code is available.
From the top of my head, did u tried something like this:
Count the lines in a file, store it. Get the size of the file (google it, i dont want to ruin the fun :D ). Then try to read from the last line u stored when size of the file changes... and again and again.
Have fun :)
Use inotify
to get notification about file changes and then reread from your last pos if the file is now larger then before.
精彩评论