文件操作 复制文件总是多个字
文件的复制操作,运行无误。但是生成的文件中,bbb_cp的最后,总会多一个“烫?” 或者“烫”,这是为什么呢?
#include<stdio.h>
#include<stdlib.h> //exit
#include<string.h> //bzero
#define BUFFER_SIZE 1024
int main(void)
{
FILE *from_fp,*to_fp;
int len_from;
char buffer[1024];
if((from_fp=fopen("d:\\aaa.txt","r"))==NULL)
{
printf("open failure of from_fp.\n");
exit(1);
}
if((to_fp=fopen("d:\\bbb_cp.txt","w"))==NULL)
{
printf("open failure of to_fp\n");
exit(1);
}
fseek(from_fp,0L,SEEK_END);
len_from=ftell(from_fp);
fseek(from_fp,0L,SEEK_SET);
printf("from file size is %d bytes\n",len_from);
while(!feof(from_fp))
{
fread(buffer,BUFFER_SIZE,1,from_fp);
if(len_from<BUFFER_SIZE)
fwrite(buffer,len_from,1,to_fp);
else
{
fwrite(buffer,BUFFER_SIZE,1,to_fp);
len_from-=BUFFER_SIZE;
}
memset(buffer,0,BUFFER_SIZE); //清空进行下一次读
}
fclose(from_fp);
fclose(to_fp);
return 0;
}