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

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

  1. FILE *fp is declared and opened with fopen in read mode (“r”) to access the file example.txt.
  2. The fseek(fp, 10, SEEK_SET) function moves the file pointer to the 10th byte from the beginning (SEEK_SET).
  3. fgetc(fp) reads the character at the current file pointer position.
  4. The character is printed using printf if the end of file (EOF) is not reached.
  5. 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

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

  1. FILE *fp is used to open example.txt in read mode.
  2. rewind(fp) resets the file pointer back to the beginning of the file.
  3. fseek(fp, 5, SEEK_CUR) moves the file pointer 5 bytes ahead from the current position (SEEK_CUR).
  4. fgetc(fp) retrieves the character at the new file pointer location.
  5. 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

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

  1. FILE *fp opens the file example.txt in read mode.
  2. fpos_t pos declares a variable to store the file position.
  3. fgetpos(fp, &pos) saves the current file pointer position (beginning of the file) into pos.
  4. fseek(fp, 15, SEEK_SET) moves the file pointer 15 bytes from the beginning (SEEK_SET).
  5. fgetc(fp) reads a character from the new position, which is then printed.
  6. fsetpos(fp, &pos) resets the file pointer back to the saved position stored in pos.
  7. Another fgetc(fp) reads the character from the original position, which is printed.
  8. 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:

  1. Using fseek to move directly to a byte offset from the beginning of the file.
  2. Combining rewind and fseek to navigate relative to the current position.
  3. Saving and restoring file positions with fgetpos and fsetpos for flexible navigation.