| | |
how to read a text file backwards?
Thread 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.
I'm here to prove you wrong.
•
•
Join Date: May 2007
Posts: 5
Reputation:
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.
I'm here to prove you wrong.
•
•
Join Date: May 2007
Posts: 5
Reputation:
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?
•
•
Join Date: May 2007
Posts: 5
Reputation:
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?
I'm here to prove you wrong.
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
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.
![]() |
Similar Threads
- Read text file to a certain point (C#)
- read text file (C)
- read text file (C#)
- using a "for" loop to read a text file (VB.NET)
Other Threads in the C Forum
- Previous Thread: Problems reading csv file into array
- Next Thread: Sort algorithm!
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






