C 中的 ftell() 用于找出文件中文件指针相对于文件开头的位置。ftell() 的语法是:
考虑下面的 C 程序。示例中的文件包含以下数据:
“Someone over there is calling you. We are going for work. Take care of yourself.” (不带引号)
执行 fscanf 语句时,单词“Someone”存储在字符串中,并且指针移动到“Someone”之外。因此 ftell(fp) 返回 7,因为“Someone”的长度为 6。
输出:假设 test.txt 包含“Someone over there ....”。
long ftell(FILE *pointer)
// C program to demonstrate use of ftel()
#include<stdio.h>
int main()
{
/* Opening file in read mode */
FILE *fp = fopen("test.txt","r");
/* Reading first string */
char string[20];
fscanf(fp,"%s",string);
/* Printing position of file pointer */
printf("%ld", ftell(fp));
return 0;
}
7