鱼C论坛

 找回密码
 立即注册
查看: 4139|回复: 10

关于fgetpos fsetpos的用法

[复制链接]
发表于 2018-9-14 21:56:22 | 显示全部楼层
彭尼玛 发表于 2018-9-14 11:48
也不是考试,主要是多会一点也好

1.png

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int main(void)
  4. {
  5.         FILE *file = fopen("test.txt", "rb");
  6.         char buf[100];

  7.         fread(buf, 1, 10, file);
  8.         for(int i = 0; i < 10; ++i)
  9.         {
  10.                 printf("buf[%d] = '%c'\n", i, buf[i]);
  11.         }

  12.         int ch;
  13.         fpos_t pos;
  14.         pos = 0;
  15.         fsetpos(file, &pos);
  16.         ch = fgetc(file);
  17.         printf("0 -> '%c'\n", ch);

  18.         pos = 3;
  19.         fsetpos(file, &pos);
  20.         ch = fgetc(file);
  21.         printf("3 -> '%c'\n", ch);

  22.         pos = 0;
  23.         fsetpos(file, &pos);
  24.         for(int i = 0; i < 3; ++i)
  25.         {
  26.                 putchar(fgetc(file));
  27.         }
  28.         printf("\n");

  29.         fgetpos(file, &pos);
  30.         printf("pos = %lld\n", pos);

  31.         fclose(file);

  32.         return 0;
  33. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-4-1 10:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表