[C/C++] 這cp…好範例
之前不小心爬到的,簡單易懂的cp指令實作範例,適合想學習C++檔案處理的人參考!
#include int main(int argc, char* argv[]) { if(argc != 3) { puts("指令: copy "); return 1; } FILE *file1 = fopen(argv[1], "rb"); FILE *file2 = fopen(argv[2], "wb"); if(!file1) { puts("檔案讀入失敗"); return 1; } if(!file2) { puts("檔案輸出失敗"); return 1; } char ch; while(!feof(file1)) { fread(&ch, sizeof(char), 1, file1); if(!feof(file1)) { fwrite(&ch, sizeof(char), 1, file2); } } fclose(file1); fclose(file2); return 0; }