转自:https://www.runoob.com/cprogramming/c-function-ftell.html
C 标准库 - <stdio.h>
描述
C 库函数 long int ftell(FILE *stream)
返回给定流 stream 的当前文件位置。
声明
下面是 ftell() 函数的声明。
long int ftell(FILE *stream)
参数
stream
– 这是指向 FILE 对象的指针,该 FILE 对象标识了流。
返回值
该函数返回位置标识符的当前值。如果发生错误,则返回 -1L,全局变量 errno 被设置为一个正值。
实例
下面的实例演示了 ftell() 函数的用法。
#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);
}
执行结果如下: