文件拷贝

来源:互联网 发布:sql语句关键字顺序 编辑:程序博客网 时间:2024/05/16 19:57
#include <stdio.h>#define MAXPATH 100int 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;}

原创粉丝点击