User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 375,170 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,182 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 6811 | Replies: 23 | Solved
Reply
Join Date: May 2007
Posts: 5
Reputation: stanats is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
stanats stanats is offline Offline
Newbie Poster

how to read a text file backwards?

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,009
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 413
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: how to read a text file backwards?

  #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.
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: May 2007
Posts: 5
Reputation: stanats is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
stanats stanats is offline Offline
Newbie Poster

Re: how to read a text file backwards?

  #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  
Join Date: Sep 2004
Posts: 6,009
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 413
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: how to read a text file backwards?

  #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.
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: May 2007
Posts: 5
Reputation: stanats is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
stanats stanats is offline Offline
Newbie Poster

Re: how to read a text file backwards?

  #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  
Join Date: Dec 2005
Posts: 3,052
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 348
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: how to read a text file backwards?

  #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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Join Date: May 2007
Posts: 5
Reputation: stanats is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
stanats stanats is offline Offline
Newbie Poster

Re: how to read a text file backwards?

  #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  
Join Date: Sep 2004
Posts: 6,009
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 413
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: how to read a text file backwards?

  #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.
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: May 2004
Posts: 177
Reputation: jim mcnamara is on a distinguished road 
Rep Power: 5
Solved Threads: 9
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: how to read a text file backwards?

  #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  
Join Date: Dec 2005
Posts: 3,052
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 348
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: how to read a text file backwards?

  #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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 11:53 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC