fseek() function in C

 fseek() function in C

fseek function in c
fseek() function

Through this article you will resolve your following doubts which are listed below,

  1. fseek() function in c
  2. header file for fseek() function in c
  3. return value of fseek() function in c
  4. fseek() function in c syntax
  5. 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
  1. SEEK_END : it denote end of the file
  2. SEEK_SET : it denote starting of the file
  3. 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;
}

____________________________________

example of fseek() function

____________________________________

Conclusion

fseek() function is basically used in file handling for repositioning the file pointer in the memory.
Post a Comment (0)
Previous Post Next Post