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

相关文章:

  • 2021-07-01
  • 2022-12-23
  • 2021-06-27
  • 2022-12-23
  • 2021-09-08
  • 2021-10-18
  • 2021-08-16
  • 2022-12-23
猜你喜欢
  • 2021-12-08
  • 2021-12-16
  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-10
相关资源
相似解决方案