| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1307 人关注过本帖
标题:question about fputc and feof
只看楼主 加入收藏
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
 问题点数:0 回复次数:13 
question about fputc and feof

I tried the following program to copy one binary file (a.txt) to another (b.txt). And found that b.txt has one extra char: 0xFF.

What is the reason?

1. source code
==============================
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
FILE *in, *out;
char ch;

if(argc != 3)
{
printf("Usage:: %s src dest\n", argv[0]);
exit(0);
}

if((in = fopen(argv[1], "rb")) == NULL)
{
printf("Cannot open file %s.", argv[1]);
exit(0);
}

if((out = fopen(argv[2], "wb")) == NULL)
{
printf("Cannot open file %s.", argv[2]);
exit(0);
}


while(!feof(in))
{
fputc(fgetc(in), out);
}

fclose(in);
fclose(out);

return 0;
}

2. a.txt and b.txt viewed in UltraEdit-32 (see picture)
=================================

图片附件: 游客没有浏览图片的权限,请 登录注册

[此贴子已经被作者于2007-8-23 13:15:28编辑过]

搜索更多相关主题的帖子: question feof fputc 
2007-08-23 13:14
雨中飞燕
Rank: 3Rank: 3
等 级:禁止访问
威 望:8
帖 子:2200
专家分:0
注 册:2007-8-9
收藏
得分:0 
while(!feof(in))
{
fputc(fgetc(in), out);
}

修改为:

int ch;
while(EOF != (ch=fgetc(in)))
{
fputc(ch, out);
}
2007-08-23 13:24
lishizelibin
Rank: 2
等 级:论坛游民
帖 子:513
专家分:41
注 册:2007-5-10
收藏
得分:0 
stdio.h里有feof的定义:
#define _IOEOF 0x0010
#define feof(_stream) ((_stream)->_flag & _IOEOF)

惟有学习不断的学习!
2007-08-23 13:41
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
回复:(雨中飞燕) while(!feof(in)) { ...

That is a neat solution.

Thanks brother 雨中飞燕.

My understanding for EOF is that the value of EOF is -1:

printf("%d\n", EOF); // outputs -1 on my machine

and when I use

fputc(-1, out); // this writes 0xFF to the output stream

I got the 0xFF since 255 = 256-1.


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-08-23 13:42
雨中飞燕
Rank: 3Rank: 3
等 级:禁止访问
威 望:8
帖 子:2200
专家分:0
注 册:2007-8-9
收藏
得分:0 
brother??晕。。
2007-08-23 13:51
百年不亮
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:789
专家分:0
注 册:2006-4-14
收藏
得分:0 
让我想起一首歌<<God is a girl>>. 雨中飞燕 a girl,you should say thanks sister


EOF通常定义为-1,也就是0xFF(char型),转化为int型就是0xFFFF,关于EOF的定义,我找到一下资料:
//////////////////////////////////////////////////////////////////////////////////////////////////
《The C programming Language》
7.1 Standard Input and Output
The symbolic constant EOF is defined in <stdio.h>. The value is typically -1, bus tests should be written in terms of EOF so as to be independent of the specific value.
//////////////////////////////////////////////////////////////////////////////////////////////////
具体值还是看实现,c语言标准没有规定具体值

在写文件时,文件结尾的EOF不用程序员写入,系统自动加的,HJin的程序在文件后面多写了一个EOF.
因为在读文件时如果超出文件结尾,FILE结构体会设置EOF标记,feof()读到该结构中的EOF标记后才返回非0,就是说HJin的程序在发现EOF之前已经超出文件结尾了。飞燕在2楼的修改可以在第一时间发现EOF,所以是合适的解决方法。


2007-08-23 17:19
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
回复:(百年不亮)让我想起一首歌
very good analysis, brother.

I do have a copy of Stroustrup's C++ and K and R's C --- but I have no patience to read them all.



I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-08-23 17:28
百年不亮
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:789
专家分:0
注 册:2006-4-14
收藏
得分:0 
I have no patience too. But I want to answer your question, I look up K and R's C just now.

2007-08-23 17:36
维c
Rank: 1
等 级:新手上路
帖 子:202
专家分:0
注 册:2007-8-13
收藏
得分:0 

But I think patience is very important for us,though I have no patience,either.


花开花落
不愁不惑
http://hi.baidu.com/vitaminic
2007-08-23 18:44
栖柏
Rank: 2
等 级:论坛游民
威 望:3
帖 子:1103
专家分:17
注 册:2007-8-23
收藏
得分:0 

stdio.h里有feof的定义:
#define _IOEOF 0x0010
#define feof(_stream) ((_stream)->_flag & _IOEOF)
你看在stdio.h已经很明确定义了
还有个_IOEOF
大家请看


You have lots more to work on! Never give up!c language!
2007-08-23 18:49
快速回复:question about fputc and feof
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030364 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved