Pointer to the file stream for which the file position is to be changed
2nd Parameter Type : long
Large integer type (4 or 8 bytes platform dependent)
2nd Parameter
The number of bytes to move the file position relative to the `whence` parameter
3rd Parameter Type : int
Integer type (typically 4 bytes, -2,147,483,648 to 2,147,483,647)
3rd Parameter
Specifies the reference point for `offset`. Possible values are `SEEK_SET`, `SEEK_CUR`, or `SEEK_END`
Read more about parameters of fseek in parameters section
The fseekfunction in C language Sets the file position of the stream to the given offset.
fseek is used for navigating within files by setting the file position indicator, essential for random access operations and non-sequential file processing. What makes it special is its flexibility with three different positioning modes (from start, from current position, or from end) and its ability to work with both binary and text files (though behavior may differ). The function is part of the file positioning family along with ftell for tracking position, rewind for returning to start, and it affects all I/O operations (like fread, fwrite, fprintf, fscanf) that depend on file position. Any positioning operation clears stream status (EOF indicator) and flushes any buffered I/O operations.
To Summarize the Workflow:
1.
Takes three parameters: stream, offset, and reference point (whence)
2.
Moves file position indicator to specified location
3.
Clears EOF indicator
4.
Returns 0 on successful repositioning
5.
Returns non-zero and sets error indicator on failure
Read more about return type and value offseek function in return section.
The fseekfunction takes 3
parameters:
•
FILE * `stream`: Pointer to the file stream for which the file position is to be changed
•
long `offset`: The number of bytes to move the file position relative to the `whence` parameter
•
int `whence`: Specifies the reference point for `offset`. Possible values are `SEEK_SET`, `SEEK_CUR`, or `SEEK_END`
The fseek function return value :
Returns 0 if successful, or non-zero if an error occurred
Output
This example demonstrates the basic usage of `fseek`. It opens a file, seeks to the 10th byte from the beginning of the file, and then reads and prints the character at that position.