Easy Tutorial
❮ C Exercise Example5 C Examples Frequency Character ❯

C Library Function - ftell()

C Standard Library - <stdio.h>

Description

The C library function long int ftell(FILE *stream) returns the current file position of the given stream.

Declaration

Here is the declaration for the ftell() function.

long int ftell(FILE *stream)

Parameters

Return Value

The function returns the current value of the position indicator. If an error occurs, it returns -1L and the global variable errno is set to a positive value.

Example

The following example demonstrates the use of the ftell() function.

#include <stdio.h>

int main ()
{
   FILE *fp;
   int len;

   fp = fopen("file.txt", "r");
   if( fp == NULL ) 
   {
      perror ("Error opening file");
      return(-1);
   }
   fseek(fp, 0, SEEK_END);

   len = ftell(fp);
   fclose(fp);

   printf("Total size of file.txt = %d bytes\n", len);

   return(0);
}

Assuming we have a text file file.txt with the following content:

This is tutorialpro.org

Let's compile and run the above program. If the file content is as shown above, this will produce the following result; otherwise, it will yield different results based on the file content:

Total size of file.txt = 19 bytes

C Standard Library - <stdio.h>

❮ C Exercise Example5 C Examples Frequency Character ❯