Latest web development tutorials

C library functions - ftell ()

C standard library - <stdio.h> C standard library - <stdio.h>

description

C library functionslong int ftell (FILE * stream) Returns the current file position of stream flow.

statement

The following is a statement ftell () function.

long int ftell(FILE *stream)

parameter

  • stream - This is a pointer to FILE pointer to an object, the object identifier FILE flow.

return value

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

Examples

The following example demonstrates ftell () function is used.

#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, which reads as follows:

This is w3cschool.cc

Let's compile and run the above program, if the contents of the file shown above, which produces the following results, otherwise it will give different results depending on the contents of the file:

file.txt 的总大小 = 21 字节

C standard library - <stdio.h> C standard library - <stdio.h>