免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 971 | 回复: 0
打印 上一主题 下一主题

一个关于C标准IO库的奇怪的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-05-15 13:00 |只看该作者 |倒序浏览
有如下代码:
  1. #include <stdio.h>;
  2. #include <stdlib.h>;

  3. int main(int argc, char *argv[])
  4. {
  5.         char line[1024];       
  6.         FILE *fp;


  7.         fp = freopen("test.txt", "a+", stdout);
  8.         if (fp == NULL)
  9.         {
  10.                 perror("freopen");
  11.                 exit(-1);
  12.         }
  13.         fprintf(stderr, "file(fp)         length = %ld\n", ftell(stdout));
  14.         fprintf(stderr, "file(stdout)         length = %ld\n", ftell(fp));
  15.         fprintf(stdout, "this is a test\n");
  16.         fflush(NULL);

  17.         fprintf(stderr, "file(fp)         length = %ld\n", ftell(stdout));
  18.         fprintf(stderr, "file(stdout)         length = %ld\n", ftell(fp));

  19.         while(fgets(line, 1024, stdout) != NULL)
  20.         {
  21.                 fprintf(stderr, "%s", line);
  22.         }

  23.         if (feof(stdout))
  24.                 fprintf(stderr, "fgets eof\n");
  25.         else
  26.         {
  27.                 fprintf(stderr, "fgets error\n");
  28.         }

  29.         exit(0);
  30. }
复制代码

当我在Linux下运行时(版本:Linux localhost.localdomain 2.2.16-3 #2
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP