ftell() in C with example
Last Updated :
07 Jun, 2023
ftell() in C is used to find out the position of the file pointer in the file with respect to starting of the file.
Syntax
The syntax of ftell() is:
long ftell(FILE *stream);
Parameters
- stream: It is the pointer to the file stream.
Return Value
- It returns a long integer value as the current position in the file.
- It returns -1 if an error occurs.
Example of ftell()
The below C program demonstrates the use of ftell() function.
C
#include <stdio.h>
int main()
{
FILE * fp = fopen ( "g4g.txt" , "r" );
char string[20];
fscanf (fp, "%s" , string);
printf ( "%ld" , ftell (fp));
return 0;
}
|
Suppose the file g4g.txt contains the following data:
g4g.txt
Someone over there is calling you. We are going for work. Take care of yourself.
Output
7
Explanation
ftell(fp) returns 7. As the length of “someone” is 7 and the character indices are from 0 to 6.
Share your thoughts in the comments
Please Login to comment...