Move to nth record of a file - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Move to nth record of a file

dubidubi Posts: 121Member
Hi friends,
I like to know how to move the file pointer(FILE *)
to the nth record or line of a file.

fseek() moves the file pointer to the number of bytes
from the file location.
I do not know how many bytes does a line(or record) has.

I read the file by fgets().
This helps in reading the file line by line.
Suppose I have moved the file pointer to 5th Line using fgets(),
now I want to move the file pointer to 2nd Line or to the 9th line
of the file.
How can I do this.

Thanks for your suggestions.


Comments

  • tsagldtsagld Posts: 621Member
    : Hi friends,
    : I like to know how to move the file pointer(FILE *)
    : to the nth record or line of a file.
    :
    : fseek() moves the file pointer to the number of bytes
    : from the file location.
    : I do not know how many bytes does a line(or record) has.
    :
    : I read the file by fgets().
    : This helps in reading the file line by line.
    : Suppose I have moved the file pointer to 5th Line using fgets(),
    : now I want to move the file pointer to 2nd Line or to the 9th line
    : of the file.
    : How can I do this.
    :
    : Thanks for your suggestions.
    :
    :
    You can't. The compiler has no knowledge of the file's structure, so there are no instructions that move a filepointer to a certain 'record'.
    The only way is to repeat fgets().
    If you want to move back and forth in the file, it may be an idea to read through the entire file once and save the position after each fgets() in an arry. After that, you can use fseek to move around.




    Greets,
    Eric Goldstein
    http://www.gvh-maatwerk.nl


  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : : Hi friends,
    : : I like to know how to move the file pointer(FILE *)
    : : to the nth record or line of a file.
    : :
    : : fseek() moves the file pointer to the number of bytes
    : : from the file location.
    : : I do not know how many bytes does a line(or record) has.
    : :
    : : I read the file by fgets().
    : : This helps in reading the file line by line.
    : : Suppose I have moved the file pointer to 5th Line using fgets(),
    : : now I want to move the file pointer to 2nd Line or to the 9th line
    : : of the file.
    : : How can I do this.
    : :
    : : Thanks for your suggestions.
    : :
    : :
    : You can't. The compiler has no knowledge of the file's structure, so there are no instructions that move a filepointer to a certain 'record'.
    : The only way is to repeat fgets().
    : If you want to move back and forth in the file, it may be an idea to read through the entire file once and save the position after each fgets() in an arry. After that, you can use fseek to move around.
    :
    :
    :
    :
    : Greets,
    : Eric Goldstein
    : http://www.gvh-maatwerk.nl
    :
    :
    :
    [blue]Once in 1992 I was given a task to view the huge (few MBytes) log files with Turbo C - I used the calloc()-ed array of LONGs to hold the line offsets! Exactly that principle...[/blue]
Sign In or Register to comment.