fseek in C
File Operations in Tamil
fseek in C
- It is used to move the reading control to different positions using fseek function.

C Syntax
fseek( file pointer, displacement, pointer position);
file pointer : It is the pointer which points to the file.
displacement : It is positive or negative. This is the number of bytes which are skipped backward (if negative) or forward( if positive) from the current position. This is attached with L because this is a long integer.
pointer position : This sets the pointer position in the file.
fseek( p,10L,0)
- 0 means pointer position is on beginning of the file, from this statement pointer position is skipped 10 bytes from the beginning of the file.
fseek( p,5L,1)
- 1 means current position of the pointer position. From this statement pointer position is skipped 5 bytes forward from the current position.
fseek(p,-5L,1)
- From this statement pointer position is skipped 5 bytes backward from the current position.
Sample Code
#include<stdio.h>
#include<conio.h>
void main() {
FILE * fp;
char ch;
int n;
fp = fopen("one.txt", "r");
if (fp == NULL) {
printf("file cannot be opened");
} else {
printf("Enter value of n to read last ānā characters");
scanf("%d", & n);
fseek(fp, -n, 2);
while ((ch = fgetc(fp)) != EOF) {
printf("%c\t", ch);
}
}
fclose(fp);
}

Output

Learn C - C tutorial - C fseek - C examples - C programs