您的位置:首页 > Web前端

利用feof()函数复制文件的C代码(解决复制后文件多一字符的问题)

2009-01-05 19:46 405 查看
/*函数名: fprintf
功 能: 传送格式化输出到一个流中
用 法: int fprintf(FILE *stream, char *format[, argument,...]);
程序例:
*/
/* Program to create backup of the
AUTOEXEC.BAT file */

#include <stdio.h>

int main(void)
{
FILE *in, *out;
int x,y,z;
char a;
x=y=z=100;

if ((in = fopen("//AUTOEXEC.BAT", "rt"))== NULL)
{
fprintf(stderr, "Cannot open input file./n");
return 1;
}

if ((out = fopen("//AUTOEXEC.BAK", "wt"))== NULL)
{
fprintf(stderr, "Cannot open output file./n");
return 1;
}

while (1)
{
x=feof(in);
a=fgetc(in); /*假设检测到文件尾部,但是feof(in)=0,in所指字符指针加1,
再去调用feof(in)返回即为非0值了*/
y=(int)a;
if(feof(in)!=0) /*解决复制文件时多一个0xFF字符的问题,即Y上加2点*/
break;
fputc(a, out);

}
fclose(in);
fclose(out);
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐