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

determine last line of file entries

P: n/a
Hi,

when I read entries in file i.e text file, how can I determine the first
line and the last line ?
I know the first line of entry can be filtered using counter, but how about
the last line of entry in EOF while loop ?

while (! file.eof() )
{
....
}

Sep 29 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
magix wrote:
Hi,

when I read entries in file i.e text file, how can I determine the first
line and the last line ?
I know the first line of entry can be filtered using counter, but how about
the last line of entry in EOF while loop ?

while (! file.eof() )
This is not standard C.
Do you mean feof(file)?
Then this is also wrong.
{
....
}
char *firstLine, *secondToLastLine, *lastLine;
Read the first line. Keep this one via firstLine. Set lastLine =
firstLine.
Try reading the second line, return on failure with firstLine and
lastLine, on success set lastLine to this second line.
Try reading the third line, on failure: as above, on success set
secondToLastLine = lastLine and afterward lastLine to the third line.
From then on:
Read line; on failure: discard secondToLastLine, return with firstLine
and lastLine; on success: discard secondToLastLine, set secondToLastLine
= lastLine and lastLine to current line.

Reading lines: I recommend fggets() from
http://cbfalconer.home.att.net/download/
Discarding lines: Means in this case free().
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 29 '06 #2

P: n/a
magix wrote:
Hi,

when I read entries in file i.e text file, how can I determine the first
line and the last line ?
I know the first line of entry can be filtered using counter, but how about
the last line of entry in EOF while loop ?

while (! file.eof() )
{
....
}
i don't know exactly what you mean by filtered. but it sounds like you
want to know which line is the first and which is the last? if so,
whats wrong with using a loop to count number of '\n'. when n = last
line then you do whatever you need to do on that line.

Sep 29 '06 #3

P: n/a

"magix" <ma***@asia.comwrote in message
news:45**********@news.tm.net.my...
Hi,

when I read entries in file i.e text file, how can I determine the first
line and the last line ?
I know the first line of entry can be filtered using counter, but how
about
the last line of entry in EOF while loop ?

while (! file.eof() )
{
....
}
This sounds a bit like someone's homework to me, so I didn't post complete
code for you... Why? Because, it's rare that one ever needs to know if it
is the last line in the file... If it is, you'll still have some work to
do. If it isn't, good luck.

This is slightly more complete and should get you started. You'll need to
add checks to make sure fopen, etc., actually worked. Also, note that feof
is being used to detect eof.

FILE *file;

strcpy(file,argv[1]);
fopen(file,"rb");
while (!feof(file))
{ /* ... */}

I almost never open a file in text mode, always as binary such as "rb" or
"wb". So, I'm slightly unsure of the following quidance work properly for
text mode...

You can use fseek/ftell to return the size of the file in bytes:

long size;

fseek(file 0L, SEEK_END);
size=ftell(file);
fseek(file 0L, SEEK_SET);

Some C functions to read data from a file tell you how many bytes were read
(i.e., fread, fscanf). If you continually add up the bytes returned by such
a function, the total bytes read in will be equal to the file size upon
reading in the last line. This will occur in your while loop prior to the
check for eof.

Also, those same functions will usually return a specific value when there
is no more input. Depending on which function you choose, the value could
be EOF, NULL or zero. I would suggest reworking the loop into a do{}while
and then using an if()break at the top of the loop to terminate the loop.
The while loop condition could be used to check that your entire read/write
buffer was filled, and the if condition would check for the eof value. If
you don't need to check if your buffer is full, you could still use the
while but change the feof check to instead check for the reading function's
returned value for EOF, NULL, or zero.

Rod Pemberton
Sep 30 '06 #4

P: n/a
"gabs" <ca********@yahoo.comwrites:
magix wrote:
>when I read entries in file i.e text file, how can I determine the first
line and the last line ?
I know the first line of entry can be filtered using counter, but how about
the last line of entry in EOF while loop ?

while (! file.eof() )
{
....
}

i don't know exactly what you mean by filtered. but it sounds like you
want to know which line is the first and which is the last? if so,
whats wrong with using a loop to count number of '\n'. when n = last
line then you do whatever you need to do on that line.
And how are you supposed to know when "n = last line"? You don't know
how many lines are in the file until you've gone past the last line
and hit end-of-file.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 2 '06 #5

P: n/a
Keith Thompson wrote:
"gabs" <ca********@yahoo.comwrites:
>magix wrote:
>>when I read entries in file i.e text file, how can I determine the first
line and the last line ?
I know the first line of entry can be filtered using counter, but how about
the last line of entry in EOF while loop ?

while (! file.eof() )
{
....
}
i don't know exactly what you mean by filtered. but it sounds like you
want to know which line is the first and which is the last? if so,
whats wrong with using a loop to count number of '\n'. when n = last
line then you do whatever you need to do on that line.

And how are you supposed to know when "n = last line"? You don't know
how many lines are in the file until you've gone past the last line
and hit end-of-file.
Maybe like this..

Let's make a couple of assumptions just to keep it simple. We have a
text file of lines, none of which are longer than say 80 characters. We
will read the file and report the number of lines and the offset to the
last line. We will use fgets() to read lines, ftell() to keep track of
where we are and fseek() to go where we want to go.

long first = 0, last;

The first line of a file will always start at offset 0.

char line[100];
int n = 0;

More than enough for an 80 character line, right?

while (fgets(line, sizeof line, fp)) {
last = ftell(fp);
++n;
}

When the loop reaches EOF, last is the offset of the last line. We can
now read and print the first line..

fseek(fp, first, SEEK_SET);
fgets(line, sizeof line, fp);
fputs(line, stdout);

And then..

fseek(fp, last, SEEK_SET);
fgets(line, sizeof line, fp);
fputs(line, stdout);

The above is not really a program. But you can make it one if you want
to. You might also use the n variable to display the line count.

I haven't tested this. Will that bite me?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Oct 3 '06 #6

P: n/a
Joe Wright <jo********@comcast.netwrites:
Keith Thompson wrote:
>"gabs" <ca********@yahoo.comwrites:
>>magix wrote:
when I read entries in file i.e text file, how can I determine
the first line and the last line ? I know the first line of
entry can be filtered using counter, but how about the last line
of entry in EOF while loop ?

while (! file.eof() )
{
....
}
i don't know exactly what you mean by filtered. but it sounds like you
want to know which line is the first and which is the last? if so,
whats wrong with using a loop to count number of '\n'. when n = last
line then you do whatever you need to do on that line.

And how are you supposed to know when "n = last line"? You don't know
how many lines are in the file until you've gone past the last line
and hit end-of-file.
Maybe like this..
[solution using ftell() and fseek() snipped]
The above is not really a program. But you can make it one if you want
to. You might also use the n variable to display the line count.

I haven't tested this. Will that bite me?
I don't see any problem with it. Of course, it won't work on a
non-seekable input stream, and if you can assume that the input lines
are no longer than N characters (you assumed 80), there's another
solution that does work with non-seekable files.

My point was that the previous poster's suggestion:

| i don't know exactly what you mean by filtered. but it sounds like you
| want to know which line is the first and which is the last? if so,
| whats wrong with using a loop to count number of '\n'. when n = last
| line then you do whatever you need to do on that line.

is at best incomplete.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 3 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.