7.5. Setting the file position indicatorΒΆ

As calls are made to read or write to a file, file position indicator is moved. These functions can be used to explicitly set the file position indicator (fpi):

rewind(FILE *fp)

  • Set the fpi to beginning of file.

fseek( FILE *fp, long offset, int place )

  • Move to offset bytes from place. place must be one of SEEK_SET, SEEK_CUR, SEEK_END which is for beginning, current, or end of the file.

long ftell(FILE *fp)

  • Returns the current file position relative to the beginning of the file.

fgetpos(FILE *fp, fpos_t *pos)

  • Like ftell() except position is stored in a data structure.

fsetpos(FILE *fp, fpos_t *pos)

  • Like fseek() except position is passed to the function in a data structure.

Here are two ways to do a bookmark.

FILE *fp;
long marker;
fpos_t altmarker;

...
marker = ftell(fp);             /* set the bookmark */
...
fseek( fp, marker, SEEK_SET );  /* move to bookmark */

fgetpos( fp, &altmarker );      /* set the bookmark */
...
fsetpos( fp, &altmarker );      /* move to bookmark */