在c语言中,ftell函数用来返回当前文件指针的位置。定义在stdio.h头文件中。
函数原型:long ftell(FILE *fp) 返回当前文件指针位置。这个位置是当前文件指针相对于文件开头的位移量。
返回值:返回文件指针的位置,若出错则返回-1L 实例:
#include <stdio.h>
int main(
void)
{
FILE *fp;
fp = fopen(
"test.txt",
"W+");
fprintf(fp,
"This is a test");
printf(
"The file pointer is at byte %ld\n", ftell(fp));
fclose(fp);
return 0;
}
注意:字符串共有14个字符,地址为0~13.调用fprintf函数后,文件指针自动移到读入的最后一个字符的下一个位置。本例中就是文件的结束符,它的地址是14.