943,816 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1445
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 15th, 2008
0

read lines from file which gets updated at same time

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
CoolAtt is offline Offline
39 posts
since Nov 2008
Dec 15th, 2008
0

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'?
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 15th, 2008
0

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.
Last edited by CoolAtt; Dec 15th, 2008 at 3:13 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
CoolAtt is offline Offline
39 posts
since Nov 2008
Dec 15th, 2008
0

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
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 15th, 2008
0

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 ?
Reputation Points: 10
Solved Threads: 0
Light Poster
CoolAtt is offline Offline
39 posts
since Nov 2008
Dec 15th, 2008
0

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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Dec 15th, 2008
0

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.
Last edited by CoolAtt; Dec 15th, 2008 at 6:19 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
CoolAtt is offline Offline
39 posts
since Nov 2008
Dec 15th, 2008
0

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 15th, 2008
0

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
Reputation Points: 10
Solved Threads: 0
Light Poster
CoolAtt is offline Offline
39 posts
since Nov 2008
Dec 16th, 2008
0

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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Printing gets me incorrecting answers
Next Thread in C Forum Timeline: how to add program to start up in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC