Jeff Rodriguez <ne********@gurugeek.EXAMPLENOSPAM.com> wrote in message news:<KIdxb.13036$o9.12259@fed1read07>...
How would one go about reading a file line by line in reverse?
For Example:
-- FILE --
1
2
3
4
5
-- FILE --
-- OUTPUT --
5
4
3
2
1
-- OUTPUT --
If someone has had experience with this please let me know, or if you have any
ideas as to how it would be done please let me know.
How big is your expected input ? If its small enough for you to
fit in memory, I'd suggest creating a linked list of "lines" (char *),
inserting new lines always at the head of the list and traversing the
list when writing out the file ...
(the following should of course be in a loop, I am illustrating
it with the loop unrolled):
list: 0
^<-head of list
read a line
list: 1 -> 0
^<-head of list
read a line
list: 2 -> 1 -> 0
^<-head of list
read a line
list: 3 -> 2 -> 1 -> 0
^<-head of list
read a line
list: 4 -> 3 -> 2 -> 1 -> 0
^<-head of list
read a line
list: 5 -> 4 -> 3 -> 2 -> 1 -> 0
^<-head of list
at this point (no more input), the head of the list should
be pointing to the node containing "5"; just traverse the list
and print out (and free, if need be) each nodes data (also free
the node).
hth
goose,
hand