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
// C program to demonstrate use of ftell() #include <stdio.h> int main()
{ /* Opening file in read mode */
FILE * fp = fopen ( "g4g.txt" , "r" );
/* Reading first string */
char string[20];
fscanf (fp, "%s" , string);
/* Printing position of file pointer */
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.
Article Tags :