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.