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



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

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