determine last line of file entries

M

magix

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() )
{
....
}
 
M

Michael Mair

magix said:
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
 
G

gabs

magix said:
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.
 
R

Rod Pemberton

magix said:
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
 
K

Keith Thompson

gabs said:
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.
 
J

Joe Wright

Keith said:
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?
 
K

Keith Thompson

Joe Wright said:
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.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top