In C, ftell() is used to determine the position of the file pointer in the file from the beginning of the file.
Syntax of fseek()
The ftell() function is declared in the <stdio.h> header file.
long ftell(FILE *stream);
Parameters
- stream: It is the pointer to the file stream.
Return Value
- Upon successful execution, It returns a long integer value which represents the current file position (in bytes) from the beginning of the file.
- It returns -1 if an error occurs.
C Program Demonstrating The Use of ftell() Function.
#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
7Explanation
In the above program the fscanf() function reads the first word from the file ('Someone') and the pointer moves forward. As the length of "someone" is 7 and the character indices are from 0 to 6. ftell(fp) returns 7.