Save and Restore Your Position in a File in C
To save and restore your position in a file in C, you can use functions such as ftell(), fseek(), rewind(), fgetpos(), and fsetpos() to manage the file pointer. These functions allow you to record the current position in a file and later return to that position.
Example 1: Using ftell() and fseek()
In this example, we open a text file, read a few characters, save the current file position using ftell(), continue reading, and then restore the file pointer back to the saved position using fseek().
main.c
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
// Read and display first 5 characters
char ch;
for (int i = 0; i < 5; i++) {
ch = fgetc(fp);
printf("%c", ch);
}
// Save current position using ftell()
long pos = ftell(fp);
printf("\nSaved Position: %ld\n", pos);
// Read next 5 characters
for (int i = 0; i < 5; i++) {
ch = fgetc(fp);
printf("%c", ch);
}
printf("\n");
// Restore the saved position using fseek()
fseek(fp, pos, SEEK_SET);
// Read again from saved position
for (int i = 0; i < 5; i++) {
ch = fgetc(fp);
printf("%c", ch);
}
printf("\n");
fclose(fp);
return 0;
}
Explanation:
- The file
example.txtis opened in read mode and its pointer is stored infp. - A loop reads the first 5 characters using
fgetc()and prints them. - The
ftell()function saves the current file pointer position into the variablepos. - Another loop reads the next 5 characters and prints them.
- The
fseek()function is used withSEEK_SETto restore the file pointer back to the saved positionpos. - A final loop reads 5 characters from the restored position and prints them, demonstrating the restoration.
Output:
ABCDE
Saved Position: 5
FGHIJ
FGHIJ
Example 2: Using rewind() to Reset File Position
In this example, we open a text file, read a portion of it, and then use rewind() to reset the file pointer back to the beginning of the file. This is useful when you need to re-read the file from the start.
main.c
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
// Read and print first 10 characters
for (int i = 0; i < 10; i++) {
printf("%c", fgetc(fp));
}
printf("\n");
// Reset file pointer to beginning using rewind()
rewind(fp);
// Read and print first 10 characters again
for (int i = 0; i < 10; i++) {
printf("%c", fgetc(fp));
}
printf("\n");
fclose(fp);
return 0;
}
Explanation:
- The file
example.txtis opened in read mode and its pointer is assigned tofp. - A loop reads and prints the first 10 characters using
fgetc(). - The
rewind()function resets the file pointer to the beginning of the file. - The same loop is executed again to read and print the first 10 characters from the start.
Output:
ABCDEFGHIJ
ABCDEFGHIJ
Example 3: Using fgetpos() and fsetpos()
In this example, we demonstrate how to save the file position using fgetpos() and restore it using fsetpos(). This approach is useful for preserving the state of the file pointer in a platform-independent way.
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;
// Read and print first 7 characters
for (int i = 0; i < 7; i++) {
printf("%c", fgetc(fp));
}
printf("\n");
// Save file position using fgetpos()
fgetpos(fp, &pos);
// Read and print next 5 characters
for (int i = 0; i < 5; i++) {
printf("%c", fgetc(fp));
}
printf("\n");
// Restore file position using fsetpos()
fsetpos(fp, &pos);
// Read and print 5 characters from the restored position
for (int i = 0; i < 5; i++) {
printf("%c", fgetc(fp));
}
printf("\n");
fclose(fp);
return 0;
}
Explanation:
- The file
example.txtis opened in read mode and assigned tofp. - The first 7 characters are read using
fgetc()and printed. fgetpos()is used to save the current file position into the variablepos.- A loop reads the next 5 characters and prints them.
fsetpos()restores the file pointer to the saved position stored inpos.- A final loop reads 5 characters from the restored position and prints them.
Output:
ABCDEFG
HIJKL
HIJKL
Conclusion
This tutorial provided multiple approaches to saving and restoring your position in a file in C. We covered:
- Using
ftell()andfseek()to record and jump back to a specific position. - Using
rewind()to reset the file pointer to the beginning. - Using
fgetpos()andfsetpos()to save and restore the file position in a platform-independent manner.
