tags:

views:

61

answers:

3

How to read the content of file while the same file is writing the log entry in thread. I wanna to develop log viewer for my tool (like tail.exe)

+1  A: 

You should not both read and write from/to the same file at the same time, you should use mutex to avoid race conditions. You can declare a global mutex variable and lock/unlock mutex while reading and writing respectevly. This is well known Producer-consumer Problem (Aka. Reader/Writer Problem).

eyazici
It simply isn't true that you cannot read and write from/to the same file at the same time. There are many streaming applications that use shared open files - e.g pause live tv - where video is being recorded and played at the same time with, possibly, a short delay. The writer does not even need to know that somebody is reading.
Dipstick
+1  A: 

You shouldn't have any problems, certainly on Linux type systems. The writer opens the file for writing. The reader opens the file for reading. The OS should make sure that all operations are atomic.

For a logging application it is probably better to use raw file i/o (open/read/write etc rather than fpen/fread etc) as the buffering doesn't help; at least make sure that the writer flushes the output so that it is written straightaway.

Dipstick
A: 

I tried using fstream in thread. It is able to read by while second writing unable to read that updated content in the file

sankaran