ftell() function in C programming is another file handling function that is used to get the current position in the file.

What is ftell() function in C?

The fseek() function which is used to position the pointer in the file, whereas ftell() function is used to get the current pointer location in the file.
ftell() function in C: – ftell() file handling function is used to get the current position of the pointer in the file.
why ftell() function is used?
To get the current position of pointer in file.

ftell function syntax: –
long ftell(FILE *fp);

In the above syntax we can see that ftell() function has one parameter which is file pointer which points to file that holds location and mode of file.
ftell returns the current location of file pointer which is long type.

ftell function Example: –
Consider file with content technical seek and we use ftell() function to get current position of pointer in file
At start when file opened.
ftell(fp)
=> This return 1 integer value as it pointing to start
Using fscanf () we read first string i.e technical
ftell(fp)
=> Now ftell returns 10 as, First string read technical and now pointing to next

Program to demonstrate ftell() function in C

#include<stdio.h>

int main()

{

FILE *fp ;

printf ("opening file in read mode");

fp=fopen("technical.txt","r"); //opening file in read mode

char string[20];

printf("\nread first string from file");

fscanf(fp,"%s",&string);. //reading first string

printf ("\n first string in file =%s,"string);

printf("\n current position in file=");

printf("%ld", ftell(fp));. //printing current position in file

return 0;

}

Program description: –

In the above program, we get current position in the file using ftell() function.

1. First, we open the file in reading mode.
Let us assume file technical.txt exist at the current directory with content (content: – Define loop)
2. First, we read the first string from the file which is technical using the fscanf function (fscanf function which is used to read the string from file)
3. Now we get current position in file using ftell function (ftell(fp)) which returns the current position i.e 10
4. Close the file.

Output: –

ftell() function in C

Write A Comment