DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   read lines from file which gets updated at same time (http://www.daniweb.com/forums/thread162314.html)

CoolAtt Dec 15th, 2008 1:56 am
read lines from file which gets updated at same time
 
hi.
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.
thx.

Murtan Dec 15th, 2008 2:34 am
Re: read lines from file which gets updated at same time
 
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'?

CoolAtt Dec 15th, 2008 2:58 am
Re: read lines from file which gets updated at same time
 
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.

Murtan Dec 15th, 2008 3:22 am
Re: read lines from file which gets updated at same time
 
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

CoolAtt Dec 15th, 2008 3:38 am
Re: read lines from file which gets updated at same time
 
will implement in C.
cant we make the fgets() wait until the updater writes some new line ?

Murtan Dec 15th, 2008 3:52 am
Re: read lines from file which gets updated at same time
 
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.

CoolAtt Dec 15th, 2008 6:19 am
Re: read lines from file which gets updated at same time
 
i tried this piece of code:
while (1)
{
fgets(buffer,1024,myfile);
printf("%s\n",buffer);
}

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

Salem Dec 15th, 2008 1:30 pm
Re: read lines from file which gets updated at same time
 
> 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.

CoolAtt Dec 15th, 2008 11:43 pm
Re: read lines from file which gets updated at same time
 
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

Murtan Dec 16th, 2008 12:15 am
Re: read lines from file which gets updated at same time
 
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.


All times are GMT -4. The time now is 7:45 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC