C中的ftell()
用C语言ftell()返回相对于文件开头的指定流的当前文件位置。在文件末尾移动文件指针后,此函数用于获取文件的总大小。它以long类型返回当前位置,并且文件可以包含32767字节以上的数据。
这是ftell()C语言的语法,
long int ftell(FILE *stream)
这是在中使用的参数ftell(),
stream-这是指向标识流的FILE对象的指针。
这是ftell()C语言的示例。
假设我们有一个包含以下内容的文件“one.txt”。
This is demo text! This is demo text! This is demo text!
现在,让我们来看一个例子。
示例
#include <stdio.h>
#include<conio.h>
void main () {
FILE *f;
int len;
f = fopen("one.txt", "r");
if(f == NULL) {
perror(“Error opening file”);
return(-1);
}
fseek(f, 0, SEEK_END);
len = ftell(f);
fclose(f);
printf("Size of file: %d bytes", len);
getch();
}输出结果
Size of file: 78 bytes