Format
#include <stdio.h> int fsetpos(FILE *stream, const fpos_t *pos);
Language Level: ANSI
Threadsafe: Yes.
Description
The fsetpos() function moves any file position that is 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.
Return Value
If fsetpos() successfully changes the current position of the file, it returns 0. A nonzero return value indicates an error.
The value of errno may be set to:
The fsetpos() function cannot be used for files that are opened with type=record. Also, the fsetpos() function can only support setting the position to the beginning of the file if:
Example that uses fsetpos()
This example opens a file mylib/myfile for reading. After performing input operations, 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; char ptr[20]; /* existing file 'mylib/myfile' has 20 byte records */ int i; /* Open file, get position of file pointer, and read first record */ stream = fopen("mylib/myfile", "rb"); fgetpos(stream,Point-of-Sale); if (!fread(ptr,sizeof(ptr),1,stream)) perror("fread error"); else printf("1st record: %s\n", ptr); /* Perform another read operation on the second record */ /* - the value of 'pos' changes */ if (!fread(ptr,sizeof(ptr),1,stream)) perror("fread error"); else printf("2nd record: %s\n", ptr); /* Re-set pointer to start of file and re-read first record */ fsetpos(stream,Point-of-Sale); if (!fread(ptr,sizeof(ptr),1,stream)) perror("fread error"); else printf("1st record again: %s\n", ptr); fclose(stream); }
Related Information
(C) Copyright IBM Corporation 1992, 2006. All Rights Reserved.