ftell()函数返回指定流的当前文件位置。将文件指针移到文件末尾后,我们可以使用ftell()函数获取文件的总大小。可以使用SEEK_END常量在文件末尾移动文件指针。
语法:
long int ftell(FILE *stream)
程序:ftell.c
#include <stdio.h>
#include <conio.h>
void main (){
FILE *fp;
int length;
clrscr();
fp = fopen("file.txt", "r");
fseek(fp, 0, SEEK_END);
length = ftell(fp);
fclose(fp);
printf("文件的大小: %d bytes", length);
getch();
}
输出:
文件的大小: 21 bytes