LinuxSir.cn,穿越时空的Linuxsir!

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

[b]一个GNU C的简单输入输出问题:为什么输出文件会多出一行?[/b]

[复制链接]
发表于 2006-7-27 16:41:51 | 显示全部楼层 |阅读模式
为什么输出文件(file.txt)会多出一行?(第20行)

源程序:copy_stdio_block.c

  1. /* a file copy program. Using library functions in stdio.h to copy one file
  2. * to another, block-by-block.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>

  6. int main()
  7. {
  8.         char s[1024];
  9.         FILE *in, *out;

  10.         in = fopen("copy_stdio.c","r");
  11.         out = fopen("file.txt", "w");
  12.         int i = 1;
  13.        
  14.         while( !feof(in) ){
  15.                 fgets(s, 1024, in);
  16.                 if( feof(in) )
  17.                         printf("line %d end of file\n", i);
  18.                 fputs(s, out);
  19.                 i = i + 1;
  20.         }

  21.         exit(0);
  22. }
复制代码

-----------------------------------------------------------------------------------
输入的文件:copy_stdio.c
http://www.linuxsir.cn/bbs/attac ... =1&d=1153988835
-----------------------------------------------------------------------------------
输出的文件:file.txt
http://www.linuxsir.cn/bbs/attac ... =1&d=1153989721
-----------------------------------------------------------------------------------
运行的过程:
http://www.linuxsir.cn/bbs/attac ... =1&d=1153989457
-----------------------------------------------------------------------------------
ps:linuxsir好像不能贴图啊,郁闷。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
发表于 2006-7-27 17:14:29 | 显示全部楼层
if( feof(in) )
printf("line %d end of file\n", i);
fputs(s, out);
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-7-27 17:19:01 | 显示全部楼层
Post by x11
if( feof(in) )
printf("line %d end of file\n", i);
fputs(s, out);

这两句有什么问题吗?
回复 支持 反对

使用道具 举报

发表于 2006-7-27 17:21:40 | 显示全部楼层
  1. /* a file copy program. Using library functions in stdio.h to copy one file
  2. * to another, block-by-block.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>

  6. int main()
  7. {
  8.         char s[1024];
  9.         FILE *in, *out;

  10.         in = fopen("test.c","r");
  11.         out = fopen("file.txt", "w");
  12.         int i = 1;

  13.         while( !feof(in) ){
  14.                 fgets(s, 1024, in);
  15.                 if( feof(in) )
  16.                 {
  17.                         printf("line %d end of file\n", i);
  18.                         [color=red]break;[/color]
  19.                 }
  20.                 fputs(s, out);
  21.                 i = i + 1;
  22.         }

  23.         exit(0);
  24. }

复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-7-27 17:40:37 | 显示全部楼层
Post by zlbruce
  1. /* a file copy program. Using library functions in stdio.h to copy one file
  2. * to another, block-by-block.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>

  6. int main()
  7. {
  8.         char s[1024];
  9.         FILE *in, *out;

  10.         in = fopen("test.c","r");
  11.         out = fopen("file.txt", "w");
  12.         int i = 1;

  13.         while( !feof(in) ){
  14.                 fgets(s, 1024, in);
  15.                 if( feof(in) )
  16.                 {
  17.                         printf("line %d end of file\n", i);
  18.                         [color=red]break;[/color]
  19.                 }
  20.                 fputs(s, out);
  21.                 i = i + 1;
  22.         }

  23.         exit(0);
  24. }

复制代码

这样文件最后部分内容会丢失的吧。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-7-27 17:52:04 | 显示全部楼层
to zlbruce:
你是对的,试了一下你的方法,问题解决了。
会不会有这个问题:开始还没的eof,fgets后feof()为真,但这时最后的一部分还没写入?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-7-27 18:25:39 | 显示全部楼层
好像fgets()读到字符串后跟的eof时并不设置eof标志,而要到一开始就读到eof才设置eof标志。是这样的吧?
感谢zlbruce。
回复 支持 反对

使用道具 举报

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

本版积分规则

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