Reading a file line by line... in reverse

J

Jeff Rodriguez

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.

Jeff
 
M

Mac

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.

Jeff

It doesn't seem like you are really asking a C question. It seems more
like you are asking a general question about an algorithm.

If you want to do this in C, you will have to open the file, read data
from it, and eventually write data back out either to the same file or to
another file.

To open a file, see fopen(). To read one character at a time, see getc().
To read in a line-oriented fashion (with limitations) see fgets(). For
writing, puts() or fputs() should do the trick for you. If you want to get
fancy and allocate buffers for lines at run time, see malloc() and free().

HTH

Mac
--
 
C

CBFalconer

Jeff said:
How would one go about reading a file line by line in reverse?

Hint: poke around in ggets.zip, available in the download section
of my site, below.
 
A

Anupam

Jeff Rodriguez said:
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.

Jeff

Hi,
The only real way to do this is to read each of the lines into an
array of strings and output in a loop traversing in reverse.
However the solution would be clearer if you had defined the problem
in a more accurate manner, more specifically please state how long
would the file be? If the file is too big, you may read chunks at a
time by saving calls to ftell() at prefixed number of lines. Then
replay the saved 'ftell'ed positions backwards to get the chunks to
rewind. When you have read such a block into an array of strings,
print each of these strings in the reverse order as the order during
the storing process.
Regards,
Anupam
 
G

goose

Jeff Rodriguez said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top