求助!关于c语言读文件的问题!

来源:百度知道 编辑:UC知道 时间:2024/05/07 22:49:34
各位大哥大姐多帮忙啊!
兄弟用c语言写了一个把2.txt中的文本复制到3.txt中的程序:
#include "stdio.h"
#include <stdlib.h>
void main()
{
FILE*out,*in;
if((in=fopen("3.txt","w"))==NULL)
printf("can not in");

if((out=fopen("2.txt","r"))==NULL)
{
printf("can not out");
exit(0);
};

while (!feof(out))
{fputc(fgetc(out),in);
}
fclose(in);
fclose(out);
}
程序运行基本正确,但是打开3.txt法相其中除了2.txt中的文本以外还有一个上面带着两个点的y!请问这是为什么?多谢各位了!

多余的了
因为!feof(out)是读到文件结束标记才停止
而循环中的fgetc(out)是先一步读到这个标记。
所以最后一个字符可能是文件结束标记的部分。
可以肯定是文件数据之后存在磁盘中的数据。

!feof(out)是读到文件结束标记才停止而fgetc(out)是先一步读到这个标记。
最后一个字符是文件结束标记的部分,是文件数据之后存在磁盘中的数据。

#include "stdio.h"
#include <stdlib.h>
void main()
{
FILE*out,*in;
char a;
if((in=fopen("3.txt","w"))==NULL)
printf("can not in");

if((out=fopen("2.txt","r"))==NULL)
{
printf("can not out");
exit(0);
};

while ((c=getc(out))!=EOF)
{putc(c,in);
}
fclose(in);
fclose(out);
}

while (!feof(out))
{fputc(fgetc(out),in);
}
明显的多于了吗。
while ((c=getc(out))!=EOF)
{fputc(c,in);
} 就行了。。