当前位置: 代码迷 >> C语言 >> question about fputc and feof
  详细解决方案

question about fputc and feof

热度:780   发布时间:2007-08-23 13:14:15.0
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  

----------------解决方案--------------------------------------------------------
while(!feof(in))
{
fputc(fgetc(in), out);
}

修改为:

int ch;
while(EOF != (ch=fgetc(in)))
{
fputc(ch, out);
}

----------------解决方案--------------------------------------------------------
stdio.h里有feof的定义:
#define _IOEOF 0x0010
#define feof(_stream) ((_stream)->_flag & _IOEOF)
----------------解决方案--------------------------------------------------------
回复:(雨中飞燕) 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.


----------------解决方案--------------------------------------------------------
brother??晕。。
----------------解决方案--------------------------------------------------------
让我想起一首歌<<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,所以是合适的解决方法。



----------------解决方案--------------------------------------------------------
回复:(百年不亮)让我想起一首歌
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 have no patience too. But I want to answer your question, I look up K and R's C just now.


----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------
  相关解决方案