Syntax
#include <stdio.h> int fsetpos(FILE *stream, const fpos_t *pos);Description
fsetpos moves any file position associated with stream to a new location within the file according to the value pointed to by pos. The value of pos was obtained by a previous call to the fgetpos library function.
If successful, fsetpos clears the end-of-file indicator, and undoes the effect of any previous ungetc function on the same stream.
After the fsetpos call, the next operation on a stream in update mode may be input or output.
If fsetpos successfully changes the current position of the file, it returns 0. A nonzero return value indicates an error.
This example opens a file myfile.dat for reading. After performing input operations (not shown), fsetpos moves the file pointer to the beginning of the file and rereads the first byte.
#include <stdio.h>
FILE *stream;
int main(void)
{
   int retcode;
   fpos_t pos,pos1,pos2,pos3;
   char ptr[20];           /* existing file 'myfile.dat' has 20 byte records  */
    /* Open file, get position of file pointer, and read first record         */
   stream = fopen("myfile.dat", "rb");
   fgetpos(stream, &pos);
   pos1 = pos;
   if (!fread(ptr, sizeof(ptr), 1, stream))
      perror("fread error");
    /* Perform a number of read operations - the value of 'pos' changes       */
    /* Re-set pointer to start of file and re-read first record               */
   fsetpos(stream, &pos1);
   if (!fread(ptr, sizeof(ptr), 1, stream))
      perror("fread error");
   fclose(stream);
   return 0;
}
Related Information