Navigate to a Specific Location Within a File in C
To navigate to a specific location within a file in C, you can use the functions such as fseek
, rewind
, and fsetpos
to adjust the file pointer.
In this tutorial, we will cover multiple examples that demonstrate different scenarios and approaches to navigate to a specific location within a file, explained step-by-step for beginners.
Example 1: Using fseek
to Move to a Specific Position
This example demonstrates how to open a file, use fseek
to navigate to a specific byte offset from the beginning, and read a character from that position.
main.c
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
// Move the file pointer 10 bytes from the beginning of the file
fseek(fp, 10, SEEK_SET);
// Read a character at the current file pointer position
int ch = fgetc(fp);
if (ch != EOF) {
printf("Character at position 10: %c\n", ch);
}
fclose(fp);
return 0;
}
Explanation:
FILE *fp
is declared and opened withfopen
in read mode (“r”) to access the fileexample.txt
.- The
fseek(fp, 10, SEEK_SET)
function moves the file pointer to the 10th byte from the beginning (SEEK_SET
). fgetc(fp)
reads the character at the current file pointer position.- The character is printed using
printf
if the end of file (EOF
) is not reached. - Finally, the file is closed with
fclose(fp)
.
Output:
Character at position 10: X
Example 2: Using rewind
and fseek
for Navigation
This example illustrates how to use rewind
to reset the file pointer to the beginning and then use fseek
to move to a position relative to the current pointer.
main.c
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
// Reset file pointer to the beginning
rewind(fp);
// Move 5 bytes from the current position (which is the beginning)
fseek(fp, 5, SEEK_CUR);
// Read a character at the new position
int ch = fgetc(fp);
if (ch != EOF) {
printf("Character at position 5: %c\n", ch);
}
fclose(fp);
return 0;
}
Explanation:
FILE *fp
is used to openexample.txt
in read mode.rewind(fp)
resets the file pointer back to the beginning of the file.fseek(fp, 5, SEEK_CUR)
moves the file pointer 5 bytes ahead from the current position (SEEK_CUR
).fgetc(fp)
retrieves the character at the new file pointer location.- The retrieved character is printed, and the file is closed using
fclose(fp)
.
Output:
Character at position 5: Y
Example 3: Using fsetpos
and fgetpos
to Navigate to a Saved Position
This example shows how to use fgetpos
to save the current file pointer position and fsetpos
to return to that position later in the file.
main.c
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
fpos_t pos;
// Save the current file pointer position (beginning of the file)
fgetpos(fp, &pos);
// Move 15 bytes from the beginning of the file
fseek(fp, 15, SEEK_SET);
// Read a character at the new position
int ch = fgetc(fp);
if (ch != EOF) {
printf("Character at position 15: %c\n", ch);
}
// Return to the saved position using fsetpos
fsetpos(fp, &pos);
// Read a character at the original position
ch = fgetc(fp);
if (ch != EOF) {
printf("Character at saved position (beginning): %c\n", ch);
}
fclose(fp);
return 0;
}
Explanation:
FILE *fp
opens the fileexample.txt
in read mode.fpos_t pos
declares a variable to store the file position.fgetpos(fp, &pos)
saves the current file pointer position (beginning of the file) intopos
.fseek(fp, 15, SEEK_SET)
moves the file pointer 15 bytes from the beginning (SEEK_SET
).fgetc(fp)
reads a character from the new position, which is then printed.fsetpos(fp, &pos)
resets the file pointer back to the saved position stored inpos
.- Another
fgetc(fp)
reads the character from the original position, which is printed. - Finally, the file is closed using
fclose(fp)
.
Output:
Character at position 15: Z
Character at saved position (beginning): A
Conclusion
This tutorial explained multiple methods to navigate to specific locations within a file in C:
- Using
fseek
to move directly to a byte offset from the beginning of the file. - Combining
rewind
andfseek
to navigate relative to the current position. - Saving and restoring file positions with
fgetpos
andfsetpos
for flexible navigation.