C library function - ftell()

Advertisements


Description

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

Declaration

Following is the declaration for ftell() function.

long int ftell(FILE *stream)

Parameters

  • stream -- This is the pointer to a FILE object that identifies the stream.

Return Value

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

Example

The following example shows the usage of 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, which has the following content:

This is tutorialspoint.com

Let us compile and run the above program, this will produce the following result:

Total size of file.txt = 27 bytes


Advertisements
Advertisements