C语言 <stdio.h> ftell() 函数

  • 描述

    C库函数long int ftell(FILE * stream)返回给定流的当前文件位置。
  • 声明

    以下是ftell()函数的声明。
    
    long int ftell(FILE *stream)
    
    参数
    • stream - 这是指向标识流的FILE对象的指针。
  • 返回值

    该函数返回位置指示器的当前值。如果发生错误,则返回-1L,并将全局变量errno设置为正值。
    示例
    以下示例显示ftell()函数的用法-
    
    #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);
    }
    
    让我们假设我们有一个文本文件file.txt,其内容如下:
    
    This is jc2182.com
    
    现在让我们编译并运行上面的程序,如果文件具有上述内容,将产生以下结果,否则将基于文件内容给出不同的结果-
    
    Total size of file.txt = 18 bytes