read lines from file which gets updated at same time

Reply

Join Date: Nov 2008
Posts: 35
Reputation: CoolAtt is an unknown quantity at this point 
Solved Threads: 0
CoolAtt's Avatar
CoolAtt CoolAtt is offline Offline
Light Poster

read lines from file which gets updated at same time

 
0
  #1
Dec 15th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: read lines from file which gets updated at same time

 
0
  #2
Dec 15th, 2008
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'?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 35
Reputation: CoolAtt is an unknown quantity at this point 
Solved Threads: 0
CoolAtt's Avatar
CoolAtt CoolAtt is offline Offline
Light Poster

Re: read lines from file which gets updated at same time

 
0
  #3
Dec 15th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: read lines from file which gets updated at same time

 
0
  #4
Dec 15th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 35
Reputation: CoolAtt is an unknown quantity at this point 
Solved Threads: 0
CoolAtt's Avatar
CoolAtt CoolAtt is offline Offline
Light Poster

Re: read lines from file which gets updated at same time

 
0
  #5
Dec 15th, 2008
will implement in C.
cant we make the fgets() wait until the updater writes some new line ?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: read lines from file which gets updated at same time

 
0
  #6
Dec 15th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 35
Reputation: CoolAtt is an unknown quantity at this point 
Solved Threads: 0
CoolAtt's Avatar
CoolAtt CoolAtt is offline Offline
Light Poster

Re: read lines from file which gets updated at same time

 
0
  #7
Dec 15th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: read lines from file which gets updated at same time

 
0
  #8
Dec 15th, 2008
> 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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 35
Reputation: CoolAtt is an unknown quantity at this point 
Solved Threads: 0
CoolAtt's Avatar
CoolAtt CoolAtt is offline Offline
Light Poster

Re: read lines from file which gets updated at same time

 
0
  #9
Dec 15th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: read lines from file which gets updated at same time

 
0
  #10
Dec 16th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC