The fseek() function is used to move the file pointer to a specified location within a file. It enables random access, allowing data to be read or written without processing the file sequentially.
- Supports movement relative to the beginning, current position, or end of a file.
- Declared in the <stdio.h> header file.
Example: The following program moves the file pointer to the end of a file.
#include <stdio.h>
int main()
{
FILE *fp = fopen("test.txt", "r");
if (fp == NULL)
return 1;
fseek(fp, 0, SEEK_END);
printf("%ld", ftell(fp));
fclose(fp);
return 0;
}
#include <stdio.h>
int main()
{
FILE *fp = fopen("test.txt", "r");
if (fp == NULL)
return 1;
fseek(fp, 0, SEEK_END);
printf("%ld", ftell(fp));
fclose(fp);
return 0;
}
Output
81Explanation: The file pointer is moved to the end of the file. The ftell() function returns the current file position, which is equal to the file size in bytes.
Syntax
int fseek(FILE *stream, long offset, int origin);
Parameters
- stream: Pointer to the file stream.
- offset: Number of bytes by which the file pointer is moved.
- origin: Starting position used to calculate the new location.
Origin Values
| Constant | Description |
|---|---|
| SEEK_SET | Beginning of the file |
| SEEK_CUR | Current file pointer position |
| SEEK_END | End of the file |
Return Value
- Returns 0 if the operation succeeds.
- Returns a non-zero value if an error occurs.
Working of fseek()
The new file position is calculated as:
New Position = origin + offset
The offset can be:
- Positive (move forward).
- Negative (move backward).
- Zero (remain at the selected origin).
Example: Move the File Pointer to the Beginning
The following example moves the file pointer to the beginning of the file.
#include <stdio.h>
int main()
{
FILE *fp = fopen("test.txt", "r");
if (fp == NULL)
return 1;
fseek(fp, 0, SEEK_SET);
printf("%ld", ftell(fp));
fclose(fp);
return 0;
}
Output
0Explanation: The file pointer is moved to the beginning of the file using SEEK_SET. Therefore, ftell() returns 0.
Example: Move the File Pointer Relative to the Current Position
The following example skips the first five characters and reads the next one.
#include <stdio.h>
int main()
{
FILE *fp = fopen("test.txt", "r");
if (fp == NULL)
return 1;
fseek(fp, 5, SEEK_SET);
printf("%c", fgetc(fp));
fclose(fp);
return 0;
}
Suppose test.txt contains:
GeeksforGeeksOutput
fExplanation: The file pointer moves 5 bytes from the beginning of the file. The next character ('f') is then read using fgetc().
Example: Move the File Pointer Relative to the Current Position
The following example moves the file pointer forward from its current location.
#include <stdio.h>
int main()
{
FILE *fp = fopen("test.txt", "r");
if (fp == NULL)
return 1;
fgetc(fp);
fgetc(fp);
fseek(fp, 3, SEEK_CUR);
printf("%c", fgetc(fp));
fclose(fp);
return 0;
}
Suppose test.txt contains:
GeeksforGeeksOutput
fExplanation: After reading two characters (G and e), the pointer advances 3 more bytes using SEEK_CUR. The next character read is 'f'.
Example: Move the File Pointer Relative to the End
The following example reads the last character of a file.
#include <stdio.h>
int main()
{
FILE *fp = fopen("test.txt", "r");
if (fp == NULL)
return 1;
fseek(fp, -1, SEEK_END);
printf("%c", fgetc(fp));
fclose(fp);
return 0;
}
Suppose test.txt contains:
GeeksforGeeksOutput
sExplanation: The file pointer moves one byte before the end of the file using SEEK_END, allowing the last character to be read.
Common Applications of fseek()
The fseek() function is widely used in file handling whenever the file pointer needs to be repositioned without reading the file sequentially.
- Reading data from a specific position in a file.
- Updating selected portions of a file.
- Determining the file size with ftell().
- Skipping unwanted data during file processing.