c语言文件拷贝
作者:互联网
实现两个文件之间的拷贝核心思想现将要拷贝的数据放到一个数组中,然后,在将数组中的数据写到要复制的文件中`:
#include<stdio.h>
#include<stdlib.h>
#define BUFSIZE 1024
int copyfile(const char* dest, const char* src) {
FILE* fp1=NULL, * fp2=NULL;
fopen_s(&fp1, dest, "r");
if (fp1 == NULL) {
return -1;
}
fopen_s(&fp2,src,"w");
if (fp2 == NULL) {
return -2;
}
char buffer[BUFSIZE];
int readlen, writelen;
while ((readlen = fread(buffer, 1, BUFSIZE, fp1))>0)
{
writelen = fwrite(buffer, 1, readlen, fp2);
if (readlen != writelen) {
return -3;
}
}
fclose(fp1);
fclose(fp2);
return 0;
}
int main() {
int ret;
ret = copyfile("a.txt","b.txt");//这里的文件简单
//的写了两个
if (ret != 0) {
switch (ret) {
case -1:
printf("打开读文件失败!\n");
break;
case -2:
printf("打开写文件失败!\n");
break;
case -3:
printf("赋值文字失败!\n");
break;
default:
printf("其他错误原因\n");
}
}
else {
printf("成功\n");
}
system("pause");
return 0;
}
标签:文件,return,语言,fp2,ret,fp1,printf,拷贝,NULL 来源: https://blog.csdn.net/weixin_49324123/article/details/110478527