FSEEK()

this function is used to move the file pointer to a specific position in a file. this is useful when there is a need to skip parts of a file or jump to a particular location for reading or writing.

SYNTAX

// Move 5 bytes from the beginning
fseek(file, 5, SEEK_SET);

// Read one character at the new position
char ch = fgetc(file);
fgetc() reads the character at that new position.


 * When a file is opened using fopen(), C creates a file pointer to track the current 
   position in the file. By default, the pointer starts at the beginning. You can move 
   this pointer manually using the fseek() function.

USAGE: READING CHARACTERS FROM A SPECIFIC POSITION

#include <stdio.h>

int main() {
    FILE *file = fopen("data.txt", "r");

    if (file == NULL) {
        printf("Error opening file.\n");
        return 1;
    }

    // Move 5 bytes from the beginning
    fseek(file, 5, SEEK_SET);

    // Read one character at the new position
    char ch = fgetc(file);
    printf("Character after seeking: %c\n", ch);

    fclose(file);
    return 0;
}

 * SEEK_SET is a macro constant defined in <stdio.h>
    - In C, SEEK_SET, SEEK_CUR, and SEEK_END are integer constants used to tell 
      fseek() where to seek from

USAGE: READING SECOND LAST CHARACTER FROM A FILE

#include <stdio.h>

void run_main_program() {
    FILE *file = fopen("/home/chef/workspace/footer.txt", "r");

    if (file == NULL) {
        printf("Error opening file.\n");
    }
    else{
        fseek(file, -2, SEEK_END); // Seek 2 bytes before end
        char ch = fgetc(file);     // Read character
        printf("Second last character: %c\n", ch);

        fclose(file);
    }

}

// Main Function
int main() {
    run_main_program();
    return 0;
}

USAGE: REVERSE PEEKING

read and print the last 3 characters of a file named tail.txt, printing each on a new line with a label. this is useful when scanning trailing file data such as status codes, delimiters, or recent entries.

#include <stdio.h>

void run_main_program() {
    FILE *file = fopen("/home/chef/workspace/tail.txt", "r");
    if (file == NULL) {
        printf("Error opening file.\n");
    }
    else{
        fseek(file, 0, SEEK_END);  // Go to end of file using seek
        long size = ftell(file);  // Find size using tell

        if (size - 1 >= 0) {
            fseek(file, -1, SEEK_END)  // Go to last character using seek 
            char ch = fgetc(file); // Read last character
            printf("Last character: %c\n", ch);
        }

        if (size - 2 >= 0) {
            fseek(file, -2, SEEK_END);  // Go to second last character using seek 
            char ch = fgetc(file); // Read second last character
            printf("Second last character: %c\n", ch);
        }

        if (size - 3 >= 0) {
            fseek(file, -3, SEEK_END);  // Go to third last character using seek 
            char ch = fgetc(file); // Read third last character
            printf("Third last character: %c\n", ch);
        }

        // Close file
        fclose(file);
    }

}

int main() {
    run_main_program();
    return 0;
}

Last updated