•
•
•
•
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
![]() |
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.
•
•
Join Date: May 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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?
>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.
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.
•
•
Join Date: May 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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?
>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?
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.
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.
•
•
Join Date: May 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
>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...
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...
>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.
And reading it line by line backward doesn't?
Member of: Beautiful Code Club.
•
•
Join Date: May 2004
Posts: 177
Reputation:
Rep Power: 5
Solved Threads: 9
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
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
> 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.
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.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Similar Threads
- read text file (C)
- read text file (C#)
- using a "for" loop to read a text file (VB.NET)
- Read text file to a certain point (C#)
Other Threads in the C Forum
- Previous Thread: Problems reading csv file into array
- Next Thread: Sort algorithm!



Linear Mode