关键词搜索

源码搜索 ×
×

ftell函数的用法(用于获取指针位置)

发布2012-11-30浏览41646次

详情内容

       ftell函数用于得到文件位置指针当前位置相对于文件首的偏移字节数,下面给出一个简单的例子:

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. FILE *fp = fopen("myData.txt", "w");
  6. cout << ftell(fp) << endl;
  7. fprintf(fp, "123");
  8. cout << ftell(fp) << endl;
  9. fprintf(fp, "%d", 456);
  10. cout << ftell(fp) << endl;
  11. fclose(fp);
  12. int a;
  13. fp = fopen("myData.txt", "r");
  14. cout << ftell(fp) << endl;
  15. fscanf(fp, "%d", &a);
  16. cout << ftell(fp) << endl;
  17. cout << a << endl;
  18. fclose(fp);
  19. cout << "*********************" << endl;
  20. int b[10];
  21. memset(b, 0, sizeof(b));
  22. fp = fopen("yourData", "wb");
  23. cout << ftell(fp) << endl;
  24. fwrite(b, sizeof(b), 1, fp);
  25. cout << ftell(fp) << endl;
  26. fclose(fp);
  27. int c[10];
  28. fp = fopen("yourData", "rb");
  29. cout << ftell(fp) << endl;
  30. fread(c, sizeof(c), 1, fp);
  31. cout << ftell(fp) << endl;
  32. fclose(fp);
  33. return 0;
  34. }


       结果为:

0
3
6
0
6
123456
*********************
0
40
0
40

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载