Open In App
Related Articles

ftell() in C with example

Improve Article
Improve
Save Article
Save
Like Article
Like

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.

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 07 Jun, 2023
Like Article
Save Article
Similar Reads