jump over lines in C

Q

questions?

I opened a file with

fileptr=fopen(filaneme,"r");

I then use

fgets(a_string, sizeof(a_string),fileptr);

to read each line.
How to jump over a prespecified number of lines without doing fgets?

Thanks for any suggestions
 
S

santosh

questions? said:
I opened a file with

fileptr=fopen(filaneme,"r");

I then use

fgets(a_string, sizeof(a_string),fileptr);

to read each line.
How to jump over a prespecified number of lines without doing fgets?

If you know the length of the lines then fseek() can be used.
Otherwise, you'll have to scan the file, character by character until
the required number of newline characters are encountered. You can then
fseek() to this position and continue your reading.
 
E

Eric Sosman

questions? said:
I opened a file with

fileptr=fopen(filaneme,"r");

I then use

fgets(a_string, sizeof(a_string),fileptr);

to read each line.
How to jump over a prespecified number of lines without doing fgets?

See the recent thread "Re: Read only last line-".
Your problem is essentially the same as the topic of
that thread.
 
E

Eric Sosman

santosh said:
If you know the length of the lines then fseek() can be used.

We've just beaten this to death in another thread, but
it seems some people weren't paying attention. It doesn't
matter whether the line lengths are known or not; either
way, fseek() is *NOT* appropriate for this purpose.
Otherwise, you'll have to scan the file, character by character until
the required number of newline characters are encountered. You can then
fseek() to this position and continue your reading.

Once you've read past the intervening lines, you're already
positioned to read the "target" line and no fseek() is needed.
 
E

Emmanuel Delahaye

questions? a écrit :
I opened a file with

fileptr=fopen(filaneme,"r");

Good. Don't forget to test fileptr against NULL before using it.
I then use

fgets(a_string, sizeof(a_string),fileptr);

Useless parens...

fgets(a_string, sizeof a_string, fileptr);
to read each line.
How to jump over a prespecified number of lines without doing fgets?

You don't. Iterate, count the fgets() and check for the value it returns
(NULL mean end of reading, whatever the reason).

Also check the presence of the '\n' in each read block to be sure it's a
complete line...

You also can read by bytes with fgetc() and count the '\n'. Probably
more a simple way...
 
J

Jordan Abel

We've just beaten this to death in another thread, but it
seems some people weren't paying attention. It doesn't matter
whether the line lengths are known or not; either way, fseek() is
*NOT* appropriate for this purpose.

Eh - whatever one might propose to do to find and save the lengths
of the lines in advance, you can do to instead save the ftell
positions of them, so it's the same class of problem. I believe this
is done for modern implementations of fortune(6) on unix systems.
 
E

Eric Sosman

Jordan said:
santosh said:
questions? wrote:
[...]
How to jump over a prespecified number of lines without doing fgets?

If you know the length of the lines then fseek() can be used.

We've just beaten this to death in another thread, but it
seems some people weren't paying attention. It doesn't matter
whether the line lengths are known or not; either way, fseek() is
*NOT* appropriate for this purpose.

Eh - whatever one might propose to do to find and save the lengths
of the lines in advance, you can do to instead save the ftell
positions of them, so it's the same class of problem. I believe this
is done for modern implementations of fortune(6) on unix systems.

Yes, if you've already read or written the file sequentially,
you can arrange to record ftell() values during the sequential pass
and use them again later on as an index that allows direct access
to the recorded positions. Still better would be to use fgetpos(),
because ftell() doesn't record (hence fseek() can't restore) the
shift states for files with multibyte characters.

But this doesn't really satisfy the O.P.'s desire to dispense
with the sequential pass altogether: at least one sequential pass
is still needed to gather the ftell() or fgetpos() data. If given
a file without an accompanying index of recorded positions/states,
straight sequential reading is the best one can (portably) do.
 
P

pete

questions? said:
I opened a file with

fileptr=fopen(filaneme,"r");

I then use

fgets(a_string, sizeof(a_string),fileptr);

to read each line.
How to jump over a prespecified number of lines without doing fgets?

Thanks for any suggestions
/*
** If the skip_n_lines function attempts
** to read past the end of the file, then rc will equal EOF.
*/
int skip_n_lines(FILE *fd, long unsigned n)
{
int rc = 0;

while (n-- != 0 && rc != EOF) {
rc = fscanf(fd, "%*[^\n]");
if (!feof(fd)) {
getc(fd);
}
}
return rc;
}
 

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

Forum statistics

Threads
474,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top