fseek() function in C
![]() |
| fseek() function |
Through this article you will resolve your following doubts which are listed below,
- fseek() function in c
- header file for fseek() function in c
- return value of fseek() function in c
- fseek() function in c syntax
- fseek() function in c program
fseek( ) function in C
The fseek() function is nothing but, it is also a standard library function of C which is basically used in file to move the file pointer associated with a given file to the specific position.
Header file for fseek( ) function in C
This file function is define in <stdio.h> which is stand for standard input-output header file.
Return value of fseek( ) function in C
fseek( ) function returns zero on success and in case of failure it returns non-zero.
- Position define the points with respect to which the file pointer need to be moved. it has three value
- SEEK_END : it denote end of the file
- SEEK_SET : it denote starting of the file
- SEEK_CUR : it denote file pointers current position
Syntax of fseek( ) function in C
int fseek(FILE *pointer , long int offset , int position):
Example of fseek( ) function in C
#include<stdio.h>
#include<stdlib.h>
struct record
{
char name[20];
int roll;
float marks;
}student;
int main()
{
int n;
FILE *fp;
fp=fopen("stu.dat","rb");
if(fp==NULL)
{
printf("Error in opening file\n");
exit(1);
}
printf("Enter the number to be read");
scanf("%d",&n);
fseek(fp,(n-1)*sizeof(student),0);
fread(&student,sizeof(student),1,fp);
printf("%s\t",student.name) ;
printf("%d\t",student.roll);
printf("%f\n",student.marks);
fclose(fp);
return 0;
}
____________________________________
____________________________________

