ftell() in C language | Detail

 ftell() in C language

ftell( ) in C language is basically used to know the current  position of file position pointer.

ftell function in c
ftell() function

Through this article we will be going to discuss ftell( ) function of C in detail in which we will cover following topics.

  1. ftell( ) function in c
  2. header file for ftell( ) function in c
  3. return value of ftell( ) function in c
  4. ftell( ) function in c syntax
  5. ftell( ) function in c program

ftell( ) function in C

The ftell( ) function is nothing but, it is a standard library function of C and it also called file handling function which is basically return current position of the stream(file). The value is counted from the beginning of the file.

Header file for ftell( ) function in C

This file function is define in <stdio.h> which is stand for standard input-output header file.

Return value of ftell( ) function in C

ftell( ) function return current position of the file on success and if error occur ftell( ) return -1L and sets an errno to positive value.

Syntax of ftell( ) function in C

long ftell(FILE *pointer):

Example of ftell( ) function in C

#include<stdio.h>
#include<stdlib.h>
struct record
{
char name [20] ;
int roll;
float marks;
}student;
int main()
{
FILE *fp;
fp=fopen("stu.dat","rb");
if(fp==NULL)
{
printf("Error in opening file\n");
exit(1);
}
printf("position pointer in the beginning->%ld\n",ftell(fp));
while(fread(&student,sizeof(student),1,fp)==1)
{
printf("Position pointer->%ld\n",ftell(fp));
printf("%s\t",student.name);
printf("%d\t",student.roll);
printf("%f\n",student.marks);
}
printf("Size of file in bytes is %ld\n",ftell(fp));
fclose(fp);
return 0;
}

____________________________________

Conclusion

ftell( ) function is basically used to know the current position of the file in memory
Post a Comment (0)
Previous Post Next Post