The fseek() function is used for setting the file pointer at the specific position in the file. Here is its syntax:
fseek(FILE *file_pointer, long int offset, int location);
This function has the following features:
- file_pointer: This represents the file pointer that points at the file.
- offset: This represents the number of bytes that the file pointer needs to be moved from the position specified by the location parameter. If the value of offset is positive, the file pointer will move forward in the file, and if it is negative, the file pointer will move backward from the given position.
- location: This is the value that defines the position from which the file pointer needs to be moved. That is, the file pointer will be moved equal to the number of bytes specified by the offset parameter from the position specified by the location parameter. Its value can be 0, 1, or 2, as shown in the following table:
Value | Meaning |
0 | The file pointer will be moved from the beginning of the file |
1 | The file pointer will be moved from the current position |
2 | The file pointer will be moved from the end of the file |
Let's look at the following example. Here, the file pointer will be moved 5 bytes forward from the beginning of the file:
fseek(fp,5L,0)
In the following example, the file pointer will be moved 5 bytes backward from the end of the file:
fseek(fp,-5L,2)