// filecopier.c -- 拷贝文件 #include <stdio.h> int file_copy(char *oldname, char *newname); int main(void){ char source[80], destination[80]; //获取源文件和目标文件文件名 puts("\nEnter source file: "); gets(source); puts("\nEnter destination file: "); gets(destination); if(file_copy(source, destination) == 0) puts("Copy operation successful"); else fprintf(stderr, "Error during copy operation"); return 0; } int file_copy(char *oldname, char *newname){ FILE *fold, *fnew; int c; // 以二进制只读模式打开源文件 if((fold = fopen(oldname, "rb")) == NULL) return -1; // 以二进制写入模式打开目标文件 if((fnew = fopen(newname, "wb")) == NULL){ fclose(fold); return -1; } /* 读取源文件内容,一次读取1字节, * 若是未达到文件末尾, * 将读取内容写入目标文件。 */ while(1){ c = fgetc(fold); if(!feof(fold)) fputc(c, fnew); else break; } fclose(fnew); fclose(fold); return 0; }