fseek -- Reposition File Position

Format

#include <stdio.h>
int fseek(FILE *stream, long int offset, int origin);

Language Level: ANSI, POSIX, XPG4
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>:

Origin Definition
SEEK_SET Beginning of file
SEEK_CUR Current position of file pointer
SEEK_END End of file

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.

Return Value
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.

Example



fgetpos -- Get File Position
fsetpos -- Set File Position
ftell -- Get Current Position
rewind -- Adjust Current File Position
ungetc -- Push Character onto Input Stream
<stdio.h>
Considerations for Programming Stream I/O