actually i hv to read lines from a file which is getting updated at the same time( new lines are appended at the end of the file).
plz suggest a way to implement it in c.

Under windows, getting the file open by two programs is a matter of using file sharing modes. The 'reader' would commonly "deny none" the 'updater' would commonly 'deny write'.

The only real concern I have is whether or not the two programs need to interact at all. How does the reader know when all of the data has been read, or does it keep trying to read in case the updater puts more data into the file?

If the second case:
How does the reader know when the data is complete?
How does the reader know that it is 'done'?

yes , it keep trying to read in case the updater puts more data into the file.
reader should stop reading the file once a date & time criteria is met.

Sounds fairly straightforward then.

Make sure you plan that the reader might be able to get a 'partial' record from the updater. It is possible that the processor might swap from one app to the other before the record is completely written.

For file sharing, look at "FileShare Enumeration" for .Net in MSDN

will implement in C.
cant we make the fgets() wait until the updater writes some new line ?

If you're planning to implement this in C, you're posting in the wrong forum. (I personally use the forum you posted your message into as a guide into how to answer the question.)

I'd have to go back and see what fgets() would do if it reached the 'end of file' without seeing a '\n'. I think it will still fetch the data from the file. As far as the reader program is concerned, the file does not have (and will not have) any more data. The libraray is not aware that more data will be written. You might even have to call some form of reset (to clear the EOF / error status) on the file in the reader to get it to read more data afterward.

i tried this piece of code:

while (1)

the prob is that wen it reaches the end of file , it keeps on printing the last line.

> the prob is that wen it reaches the end of file , it keeps on printing the last line.
You ignore the return result of fgets()

You could try detecting that
- fgets() returns NULL
- then sleeping for a while, in the hope the file will grow
- call clearerr() to reset the EOF condition.

ok will do that.
instead of making the program sleep, will it be ok to check everytime in an infinite loop for the following:

if fgets is not null -> read & process

No, you would be MUCH better off sleeping. If you don't sleep the CPU shows 100% utilization and things slow down, including the other program trying to write the data.

I would start the sleep at 1 second (1000 ms) and wouldn't reduce it unless the performance required it.

ok. plz give an overview.
the sequence of steps i mean.
is my understanding correct ?


if fgets == NULL{
read & process string


That looks pretty close.

Remember that just because fgets() did not return NULL does NOT mean that it read a complete input line. (It is possible for fgets to return with a partial line of data if there is a partial line of data at the end of the file.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.