Open In App

ftell() in C with example

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

Return Value

Example of ftell()

The below C program demonstrates the use of ftell() function.




// 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 :