📅  最后修改于: 2020-09-25 08:24:59             🧑  作者: Mango
long ftell(FILE* stream);
ftell()
函数将文件流作为其参数,并以long int类型返回给定流的文件位置指示符的当前值。
它在
stream
:返回其当前位置的文件流。
成功后, ftell()
函数将返回文件位置指示符。否则,它返回-1L。
#include
#include
using namespace std;
int main()
{
int pos;
char c;
FILE *fp;
fp = fopen("file.txt", "r");
if (fp)
{
while ((c = getc(fp)) != EOF)
{
pos = ftell(fp);
cout << "At position " << pos << ", character is " << c << endl;
}
}
else
{
perror("Error reading file");
}
fclose(fp);
return 0;
}
运行该程序时,输出为:
At position 1, character is P
At position 2, character is r
At position 3, character is o
At position 4, character is g
At position 5, character is r
At position 6, character is a
At position 7, character is m
At position 8, character is i
At position 9, character is z
At position 10, character is .
At position 11, character is c
At position 12, character is o
At position 13, character is m