C++官网参考链接:https://cplusplus.com/reference/cstdio/ftell/
函数
<cstdio>
ftell
long int ftell ( FILE * stream );
获取流中的当前位置
返回stream的位置指示符的当前值。
对于二进制的stream,这是从文件开始的字节数。
对于文本stream,这个数值可能没有意义,但仍然可以用于稍后使用fseek(如果使用ungetc放回的字符仍未被读取,则行为未定义)将位置恢复到相同的位置。
形参
stream
指向标识流的FILE对象的指针。
返回值
如果成功,则返回位置指示符的当前值。
如果失败,将返回-1L,并将errno设置为特定于系统的正数值。
用例
/* ftell example : getting size of a file */
#include
int main ()
{
FILE * pFile;
long size;
pFile = fopen ("myfile.txt","rb");
if (pFile==NULL) perror ("Error opening file");
else
{
fseek (pFile, 0, SEEK_END); // non-portable
size=ftell (pFile);
fclose (pFile);
printf ("Size of myfile.txt: %ld bytes.\n",size);
}
return 0;
}
这个程序打印出myfile.txt的大小,以字节为单位(在支持的地方)。

