#include <stdio.h>
#define MAXPATH 100
int CopyFile(const char* srcName,const char* dstName);
int main()
{
char srcFilename[MAXPATH];
char dstFilename[MAXPATH];
printf("The source filename:");
scanf("%s",srcFilename);
printf("The destination filename:");
scanf("%s",dstFilename);
if (CopyFile(srcFilename,dstFilename))
{
printf("Copy succeed.\n");
}
else
{
perror("Copy failed.");
}
}
int CopyFile(const char* srcName,const char* dstName)
{
FILE *fpSrc=NULL;
FILE *fpDst=NULL;
int ch,rval=1;
fpSrc=fopen(srcName,"rb");
if (fpSrc==NULL)
{
goto ERROR;
}
fpDst=fopen(dstName,"wb");
if (fpDst==NULL)
{
goto ERROR;
}
while ((ch=fgetc(fpSrc))!=EOF)
{
if(fputc(ch,fpDst)==EOF)
goto ERROR;
}
fflush(fpDst);
goto EXIT;
ERROR:
rval=0;
EXIT:
if (fpSrc!=NULL)
{
fclose(fpSrc);
}
if (fpDst!=NULL)
{
fclose(fpDst);
}
return rval;
}
文件拷贝
最新推荐文章于 2025-03-07 15:28:15 发布