ftell函数用于得到文件位置指针当前位置相对于文件首的偏移字节数,在随机方式存储文件时,由于文件位置频繁的前后移动,程序不容易确定文件的当前位置。
/***
a.txt
***/
asd
gsdert
dfhjtew
/***
ftell.c
***/
#include<stdio.h>
int main()
{
FILE *p = fopen("./a.txt","rb");
fseek(p,2,SEEK_SET);
char buf[100] = {0};
fgets(buf,sizeof(buf),p);
printf("buf = %s",buf);
printf("ftell = %ld\n",ftell(p));
fclose(p);
return 0;
}
运行结果:
ubuntu1604@ubuntu:~/wangqinghe/C/20190727$ ./ftell
buf = d
ftell = 4 (想想为啥?)
/***
ftell.c
***/
#include<stdio.h>
int main()
{
FILE *p = fopen("./a.txt","rb");
fseek(p,2,SEEK_SET);
char buf[100] = {0};
fgets(buf,sizeof(buf),p);
printf("buf = %s",buf);
fgets(buf,sizeof(buf),p);
printf("buf = %s",buf);
printf("ftell = %ld\n",ftell(p));
fclose(p);
return 0;
}
运行结果:
ubuntu1604@ubuntu:~/wangqinghe/C/20190727$ ./ftell
buf = d
buf = gsdert
ftell = 11 (想想为啥?)