By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,721 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

Reading a file line by line... in reverse

P: n/a
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

Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Mac
On Wed, 26 Nov 2003 19:56:33 +0000, Jeff Rodriguez wrote:
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
--

Nov 13 '05 #2

P: n/a
Jeff Rodriguez wrote:

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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 13 '05 #3

P: n/a
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.

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
Nov 13 '05 #4

P: n/a
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
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.