fseek(), ftell(), rewind() in C

File pointer movement functions allow you to control the position of the file pointer so you can read/write from anywhere in a file. These functions are used for **random file access**, skipping bytes, going backward, and restarting the file pointer.

Understanding File Pointer Movement

FILE POINTER MOVEMENT DIAGRAM:

|--- FILE START ---|------- DATA ---------|----- END -----|
^
0 position

fseek(fp, 0, SEEK_SET); β†’ Move to start  
fseek(fp, 10, SEEK_CUR); β†’ Move forward 10 bytes  
fseek(fp, -5, SEEK_END); β†’ Move backward 5 bytes

1. fseek()

Prototype: int fseek(FILE *fp, long offset, int position);

2. ftell()

Returns the current pointer position (in bytes).

long pos = ftell(fp);  

3. rewind()

Moves pointer to the beginning (same as fseek(fp, 0, SEEK_SET)).

rewind(fp);
Important: These functions are used for random access in files like logs, indexes, student database, etc.

10 Practical Examples

#include <stdio.h>

/* Example 1 – fseek to beginning */
int main() {
    FILE *fp = fopen("data.txt", "r");
    fseek(fp, 0, SEEK_SET);
    printf("Moved to start");
    fclose(fp);
    return 0;
}
#include <stdio.h>

/* Example 2 – fseek to specific byte */
int main() {
    FILE *fp = fopen("data.txt", "r");
    fseek(fp, 5, SEEK_SET);
    printf("Pointer at 5th byte");
    fclose(fp);
    return 0;
}
#include <stdio.h>

/* Example 3 – Read last 5 characters */
int main() {
    FILE *fp = fopen("data.txt", "r");
    fseek(fp, -5, SEEK_END);
    char ch;
    while((ch = fgetc(fp)) != EOF)
        putchar(ch);
    fclose(fp);
    return 0;
}
#include <stdio.h>

/* Example 4 – ftell position */
int main() {
    FILE *fp = fopen("data.txt", "r");
    fgetc(fp);
    long pos = ftell(fp);
    printf("Position: %ld", pos);
    fclose(fp);
    return 0;
}
#include <stdio.h>

/* Example 5 – rewind() usage */
int main() {
    FILE *fp = fopen("data.txt", "r");
    fseek(fp, 10, SEEK_SET);
    rewind(fp);
    printf("Pointer reset to start");
    fclose(fp);
    return 0;
}
#include <stdio.h>

/* Example 6 – Skip first 10 bytes */
int main() {
    FILE *fp = fopen("data.txt", "r");
    fseek(fp, 10, SEEK_SET);
    int ch = fgetc(fp);
    printf("Next character: %c", ch);
    fclose(fp);
    return 0;
}
#include <stdio.h>

/* Example 7 – Go back 3 characters */
int main() {
    FILE *fp = fopen("data.txt", "r");
    fseek(fp, 0, SEEK_END);
    fseek(fp, -3, SEEK_CUR);
    printf("Moved 3 positions back");
    fclose(fp);
    return 0;
}
#include <stdio.h>

/* Example 8 – Get file size using fseek + ftell */
int main() {
    FILE *fp = fopen("data.txt", "r");
    fseek(fp, 0, SEEK_END);
    long size = ftell(fp);
    printf("File Size: %ld bytes", size);
    fclose(fp);
    return 0;
}
#include <stdio.h>

/* Example 9 – fseek inside midpoint */
int main() {
    FILE *fp = fopen("data.txt", "r");
    fseek(fp, 0, SEEK_END);
    long mid = ftell(fp) / 2;
    fseek(fp, mid, SEEK_SET);
    printf("Pointer moved to middle");
    fclose(fp);
    return 0;
}
#include <stdio.h>

/* Example 10 – Overwrite specific position */
int main() {
    FILE *fp = fopen("data.txt", "r+");
    fseek(fp, 5, SEEK_SET);
    fputc('*', fp);
    printf("Character replaced");
    fclose(fp);
    return 0;
}