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
- fseek() β Move file pointer to any position
- ftell() β Returns current position of pointer
- rewind() β Resets pointer to beginning
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);
- SEEK_SET β Start of file
- SEEK_CUR β Current position
- SEEK_END β End of file
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;
}