如何实现:对用户输入的文件名的内容进行拷贝。
如题,例如: %EXE aFile bFile 则将文件aFile 的 内容拷贝到文件bFile中。
#include<stdio.h> int main(int argc, char *argv[]) { FILE *f1, *f2; long size;//记录文件大小 if(argc != 3) { printf("usage: *.out (filename1 filename2)\n"); return 0; } if((f1 = fopen(argv[1], "rb")) == NULL) { printf("cannot open %s\n", argv[1]); return 0; } if((f2 = fopen(argv[2], "wb")) == NULL) { printf("cannot open %s\n", argv[2]); fclose(f1); return 0; } fseek(f1,0,SEEK_END); size = ftell(f1); fseek(f1,0,SEEK_SET); while(size--) { fputc(fgetc(f1), f2); } fclose(f1); fclose(f2); return 1; }
#include<stdio.h> int main(int argc, char *argv[]) { FILE *f1, *f2; if(argc != 3) { printf("usage: *.out (filename1 filename2)\n"); return 0; } if((f1 = fopen(argv[1], "rb")) == NULL) { printf("cannot open %s\n", argv[1]); return 0; } if((f2 = fopen(argv[2], "wb")) == NULL) { printf("cannot open %s\n", argv[2]); fclose(f1); return 0; } while(!feof(f1)) { fputc(fgetc(f1), f2); } fclose(f1); fclose(f2); return 1; }