C语言实现文件复制

来源:互联网 发布:linux性能监控命令 编辑:程序博客网 时间:2024/04/27 17:01

#include <stdio.h>#include <stdlib.h>int copyFile(const char* dest, const char* src){FILE* fin = fopen(dest, "r");FILE* fout = fopen(src, "w");if (fin && fout){while (!feof(fin))//读到最后一个字符,feof(fin)仍未false{fputc(fgetc(fin), fout);}fclose(fin);fclose(fout);return 0;}return -1;}int main(int argc, char* argv[]){if (argc > 2){if (copyFile(argv[1], argv[2]) != 0)printf("文件复制失败\n");else{long long len;FILE *fp;fp = fopen(argv[1],"r");fseek(fp,0L,SEEK_END);len = ftell(fp);rewind(fp);printf("输入文件大小:%lld 字节\n",len);printf("文件内容:\n");while (!feof(fp)){putchar(fgetc(fp));}printf("\n");fclose(fp);fp = fopen(argv[2],"r");fseek(fp,0L,SEEK_END);len = ftell(fp);rewind(fp);printf("输出文件大小:%lld 字节\n",len);printf("文件内容:\n");while (!feof(fp)){putchar(fgetc(fp));}printf("\n");fclose(fp);}}}

命令行:

copy.exe in.txt out.txt

注意out.txt读入了in.txt的文件结束标志EOF,使得两个文件的大小不一样!






原创粉丝点击