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

</>
Copy
#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:

  1. The file example.txt is opened in read mode and its pointer is stored in fp.
  2. A loop reads the first 5 characters using fgetc() and prints them.
  3. The ftell() function saves the current file pointer position into the variable pos.
  4. Another loop reads the next 5 characters and prints them.
  5. The fseek() function is used with SEEK_SET to restore the file pointer back to the saved position pos.
  6. 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

</>
Copy
#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:

  1. The file example.txt is opened in read mode and its pointer is assigned to fp.
  2. A loop reads and prints the first 10 characters using fgetc().
  3. The rewind() function resets the file pointer to the beginning of the file.
  4. 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

</>
Copy
#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:

  1. The file example.txt is opened in read mode and assigned to fp.
  2. The first 7 characters are read using fgetc() and printed.
  3. fgetpos() is used to save the current file position into the variable pos.
  4. A loop reads the next 5 characters and prints them.
  5. fsetpos() restores the file pointer to the saved position stored in pos.
  6. 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:

  1. Using ftell() and fseek() to record and jump back to a specific position.
  2. Using rewind() to reset the file pointer to the beginning.
  3. Using fgetpos() and fsetpos() to save and restore the file position in a platform-independent manner.