Syntax
#include <stdio.h> int fseek(FILE *stream, long int offset, int origin);Description
fseek changes the current file position associated with stream to a new location within the file. The next operation on the stream takes place at the new location. On a stream open for update, the next operation can be either a reading or a writing operation.
The origin must be one of the following constants defined in <stdio.h>: compact break=fit.
Origin
For a binary stream, you can also change the position beyond the end of the file. An attempt to position before the beginning of the file causes an error. If successful, fseek clears the end-of-file indicator, even when origin is SEEK_END, and undoes the effect of any preceding ungetc function on the same stream.
Note: For streams opened in text mode, fseek has limited use because some system translations (such as those between carriage-return-line-feed and new line) can produce unexpected results. The only fseek operations that can be relied upon to work on streams opened in text mode are seeking with an offset of zero relative to any of the origin values or seeking from the beginning of the file with an offset value returned from a call to ftell. See the chapter "Performing I/O Operations" in the VisualAge C++ Programming Guide for more information.
fseek returns 0 if it successfully moves the pointer. A nonzero return value indicates an error. On devices that cannot seek, such as terminals and printers, the return value is nonzero.
This example opens a file myfile.dat for reading. After performing input operations (not shown), fseek moves the file pointer to the beginning of the file.
#include <stdio.h>#include <stdlib.h> int main(void) { FILE *stream; int result; stream = fopen("myfile.dat", "r"); result = fseek(stream, 0L, SEEK_SET); /* moves the pointer to the beginning of the file */ if (result) /* fail to move the pointer to the */ return EXIT_FAILURE; /* beginning of the file */ fclose(stream); return 0; }Related Information