Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - ftell()

C Library Function - ftell()

C Standard Library - <stdio.h> C Standard Library -

Description

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

Declaration

Below is the declaration of the ftell() function.



long int ftell(FILE *stream)

Parameters

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 use of the ftell() function.



#include 



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 file file.txt, with the following content:



This is runoob.com

Let's compile and run the above program; if the file contents are as shown above, this will produce the following result; otherwise, it will give a different result depending on the file contents:



file.txt 的总大小 = 19 字节

C Standard Library - <stdio.h> C Standard Library -