C library functions -ftell()

C Standard Library - <stdio.h>

Description

C Library Functionslong int ftell(FILE *stream)Returns the current file position of the given stream.

Declaration

The following is the declaration of the ftell() function.

long int ftell(FILE *stream)

Parameter

  • stream-- This is a 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, it returns -1L and the global variable errno is set to a positive value.

Example

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

#include <stdio.h>

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

   fp = fopen("file.txt", "r");
   if( fp == NULL ) 
   {
      perror ("打开文件错误");
      return(-1);
   }
   fseek(fp, 0, SEEK_END);

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

   printf("file.txt 的总大小 = %d 字节\n", len);
   
   return(0);
}

Suppose we have a text filefile.txt, whose content is as follows:

This is example.com

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

file.txt 的总大小 = 19 字节

C Standard Library - <stdio.h>

other extensions