标签:
1 #include "stdafx.h" 2 #include <stdio.h> 3 #include <stdlib.h> 4 #define N 80 5 int CopyFile(const char *srcName,const char *dstName); 6 int main() 7 { 8 char srcFilename[N]; 9 char dstFilename[N]; 10 printf_s("input source filename:"); 11 scanf_s("%s",srcFilename,sizeof(srcFilename)); 12 printf_s("Input destination filename:"); 13 scanf_s("%s",dstFilename,sizeof(dstFilename)); 14 if(CopyFile(srcFilename,dstFilename)) 15 printf_s("Copy succeed!\n"); 16 else 17 printf_s("Copy failed!\n"); 18 system("pause"); 19 return 0; 20 } 21 int CopyFile(const char *srcName,const char *dstName) 22 { 23 FILE *fpSrc = NULL,*fpDst = NULL; 24 int ch, rval =1; 25 if(fopen_s(&fpSrc,srcName,"r")) 26 goto ERROR; 27 if(fopen_s(&fpDst,dstName,"w")) 28 goto ERROR; 29 while((ch = fgetc(fpSrc)) != EOF) 30 { 31 if(fputc(ch,fpDst) == EOF) 32 goto ERROR; 33 } 34 fflush(fpDst); 35 goto EXIT; 36 ERROR : 37 rval = 0; 38 EXIT: 39 if(fpSrc != NULL) fclose(fpSrc); 40 if(fpDst != NULL) fclose(fpDst); 41 return rval; 42 }
测试
1,Input source filename : a.txt (a文件存在)
Input destination filename : b.txt
Copy succeed!
2,
Input source filename : a.txt (a文件不存在)
Input destination filename : b.txt
Copy failed!
复制文件。输入完整已存在的文本文件名,再输入一个新文本文件的完整文件名,利用文本编辑软件查看文件内容,验证程序执行结果
标签:
原文地址:http://www.cnblogs.com/joyclub/p/4437059.html