LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1019|回复: 9

文件结束符。。。。

[复制链接]
发表于 2003-10-30 18:18:22 | 显示全部楼层 |阅读模式

  1. #include <stdio.h>
  2. main(void)
  3. {
  4.   FILE *fpi;
  5.   char ch=0;
  6.                                                                                 
  7. fpi=fopen("http.c","r");
  8.                                                                                 
  9. for(;ch!=EOF ; )
  10.     {
  11.                                                                                 
  12.         fscanf(fpi,"%c",&ch) ;
  13.         printf("%c",ch);
  14.      }
  15. fclose(fpi);
  16. }
复制代码


错在哪里。。。

运行了不能停下来
。。。。。
发表于 2003-10-30 20:05:58 | 显示全部楼层
问题出在 fscanf(fpi,"%c",&ch); 函数,具体搞不清为什么,换成其它就好了。
还有 EOF 一般等于 -1,  char ch=0; 放不下,改成  int ch = 0;

  1. for(;ch!EOF;)
复制代码

这种结构不太美观,用  while(ch!=EOF) 或 do {} while (xxxx) 要好点。
发表于 2003-10-30 20:36:22 | 显示全部楼层
#include <stdio.h>
main(void)
{
  FILE *fpi;
  char ch=0;
  int res = 0;
                                                                                
fpi=fopen("http.c","r");
                                                                                
for(;res!=EOF; )
    {
                                                                                
        res = fscanf(fpi,"%c",&ch);
        printf("%c",ch);
     }
fclose(fpi);
}
 楼主| 发表于 2003-10-30 21:14:10 | 显示全部楼层
谢了。。。
 楼主| 发表于 2003-10-30 21:42:48 | 显示全部楼层
不会输出行符。。


  1. #include <stdio.h>
  2. main(void)
  3. {
  4. FILE *fpi;
  5. char ch=0;
  6. int res = 0;
  7.                                                                                 
  8. fpi=fopen("http.c","r");
  9.                                                                                 
  10. res=fscanf(fpi,"%c",&ch);
  11.                                                                                 
  12. for(;res!=EOF; )
  13. {
  14.    printf("%c",ch);
  15.    res=fscanf(fpi,"%c",&ch);
  16. }
  17. fclose(fpi);
  18. }

复制代码
发表于 2003-10-30 22:26:08 | 显示全部楼层
EOF是不保存在文件中的
所以想从文件中读是读不出来的

http://loveunix.onlinecq.com/index.php?showtopic=4298
在上面地址我也说明了一下
发表于 2003-10-31 08:46:47 | 显示全部楼层
qing,那段代码是不是老的unix书上的?据说很老的unix上的文本文件最后是有eof的表示结束的。不知哪位说一下?谢谢!
发表于 2003-10-31 12:55:06 | 显示全部楼层
为什么用 fgetc 代替 fscanf 就没问题了呢?而其它都不变。
发表于 2003-10-31 14:47:07 | 显示全部楼层
最初由 quanliking 发表
为什么用 fgetc 代替 fscanf 就没问题了呢?而其它都不变。

fgetc在到达文件尾或出错时返回EOF.
fscanf会一直等到有字符来到,才返回。
发表于 2003-10-31 23:17:56 | 显示全部楼层
据说很老的unix上的文本文件最后是有eof的表示结束的。不知哪位说一下?谢谢
No
you are wrong
the char is from 0-255
if the file is a binary file
then these value may occur in it

so ,write a char in the file to indicate EOF is impossable
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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