how to read a text file backwards?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2007
Posts: 5
Reputation: stanats is an unknown quantity at this point 
Solved Threads: 0
stanats stanats is offline Offline
Newbie Poster

how to read a text file backwards?

 
0
  #1
May 15th, 2007
i'm having this problem and i would like to ask for some ideas on how i will read a file backwards, that is, from end, read the file backwards(forward, naturally) by line..

thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: how to read a text file backwards?

 
0
  #2
May 15th, 2007
Read it forward, store it in a reversible container, and then walk through the container backward. Or if you know the exact length of each line, you could open the file as binary and use fseek with read to get the lines in reverse. Or you could physically reverse the file before reading it. Or you could use recursion, but that's an exceptionally poor solution.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 5
Reputation: stanats is an unknown quantity at this point 
Solved Threads: 0
stanats stanats is offline Offline
Newbie Poster

Re: how to read a text file backwards?

 
0
  #3
May 15th, 2007
Originally Posted by Narue View Post
Read it forward, store it in a reversible container, and then walk through the container backward. Or if you know the exact length of each line, you could open the file as binary and use fseek with read to get the lines in reverse. Or you could physically reverse the file before reading it. Or you could use recursion, but that's an exceptionally poor solution.


what container would you suggest? is it possible to use some kind of strtok but backwards?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: how to read a text file backwards?

 
0
  #4
May 15th, 2007
>what container would you suggest?
A linked list since you might not know how many lines are in the file.

>is it possible to use some kind of strtok but backwards?
No, the trick here is that you have to get the lines from the file into memory before you can do anything with them. So your problem boils down to either reading the entire file into memory, or using tricky seeking to actually read the file from end to beginning.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 5
Reputation: stanats is an unknown quantity at this point 
Solved Threads: 0
stanats stanats is offline Offline
Newbie Poster

Re: how to read a text file backwards?

 
0
  #5
May 15th, 2007
reading it line by line forward costs too much...

>No, the trick here is that you have to get the lines from the file into memory before you can do anything with them. So your problem boils down to either reading the entire file into memory, or using tricky seeking to actually read the file from end to beginning.

how would that tricky seeking be implemented?
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: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: how to read a text file backwards?

 
0
  #6
May 16th, 2007
If the text file is large (and constant), and you need to do this a lot, then reading the whole file once to produce an index is worthwhile.

The index being all the 'tell' positions for the start of each line, so at some future time, you can just 'seek' to the start of any given line and read it.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 5
Reputation: stanats is an unknown quantity at this point 
Solved Threads: 0
stanats stanats is offline Offline
Newbie Poster

Re: how to read a text file backwards?

 
0
  #7
May 16th, 2007
>If the text file is large (and constant), and you need to do this a lot, then reading the whole file once to produce an index is worthwhile.

the text file is large and not constant...the program constantly adds entries to the file, and the most recent is at the bottom, that is what I wanted to read first and not from the head...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: how to read a text file backwards?

 
0
  #8
May 16th, 2007
>reading it line by line forward costs too much...
And reading it line by line backward doesn't? Regardless of your solution, you've got the penalty of reading the file. Unless it's a random access file (unlikely), that involves sequential front-to-back access. Oh, and have you tried any of the suggestions and proven that they all cost too much? I'm willing to bet that you haven't.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: how to read a text file backwards?

 
0
  #9
May 16th, 2007
Consider calling stat() to get the filesize first. Open the file, then move the file pointer forward to some arbitrary place.

If you assume no last line is ever longer than say, 200 characters,
then fseek() to within 200 character size units of the end of the file.
fread in the last bit of the file into a buffer.

Now you can apply what Narue indicated - you have a container that you can "backwards read" from to get the last line of the file. If you need the last 10 lines, then adjust the file pointer positioning scheme.

If you want to see a generalized algorithm for this, consult the coreutils source for the "tail" utility at
www.gnu.org
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: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: how to read a text file backwards?

 
0
  #10
May 16th, 2007
> that is what I wanted to read first and not from the head
Seems to me like you want the 'tail' program.

Or in any event, read forwards from the point you last read to get any new data in the file.

In which case, the answer still stands. You record the 'tell' position of where you got to, then 'seek' to it sometime later, and carry on reading.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 13921 | Replies: 23
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC