473,385 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reading a file line by line... in reverse

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
4 7233
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: xEM | last post by:
how can i read for example 10 last lines from a text file beginning from last line? do you have some idea? in a different manner... how can i set file position indicator one line upper in text...
14
by: Erik Andersson | last post by:
Hi! I need to read a file (line-by-line) in reverse order, without reading the whole file into memory first. Is there an easy way of doing this? As in, is there a PHP function I could use? ...
14
by: Kevin Knorpp | last post by:
Hello. I need to be able to extract the data from the attached file (or any file in the same format) so that I can work with the data in PHP. I'm fairly comfortable with using PHP with...
7
by: Jay | last post by:
I have a very large text file (being read by a CGI script on a web server), and I get memory errors when I try to read the whole file into a list of strings. The problem is, I want to read the file...
7
by: Shahid Juma | last post by:
Hi, I have a text file which I would like to read from the end and display only a certain number of records. Is there any way of doing this? Thanks, Shahid
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
6
by: Neil Patel | last post by:
I have a log file that puts the most recent record at the bottom of the file. Each line is delimited by a \r\n Does anyone know how to seek to the end of the file and start reading backwards?
11
by: JWest46088 | last post by:
I'm having difficulty trying to figure out how to print a text file from a hash table one line at a time. I have the text file read into the hash table and can print the text file all at once, but I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.