| | |
how to read a text file backwards?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
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.
New members chased away this month: 4
•
•
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.
New members chased away this month: 4
•
•
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?
New members chased away this month: 4
•
•
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!
Views: 13921 | Replies: 23
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o inches infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape socketprograming spoonfeeding stack standard strchr string strings structures student suggestions system systemcall test testautomation unix user voidmain() wab win32 win32api windows.h






