C語言實現複製一個檔案內容到另一個檔案裡
阿新 • • 發佈:2019-02-03
首先你的建立兩個txt檔案或者其他檔案也行,然後在第一個檔案中寫一些內容 第二個檔案中不用寫
我的兩個檔案是test1.txt test2.txt 執行程式後test1.txt的內容被複制到test2.txt
#include<stdio.h> int main() { char ch; FILE* pfr = fopen("C:\\Users\\ljh\\Desktop\\test1.txt", "r");//把路徑改成你檔案的路徑 FILE* pfw = fopen("C:\\Users\\ljh\\Desktop\\test2.txt", "w");//把路徑改成你檔案的路徑 if (NULL == pfw) { perror("open file test2.txt"); } if (NULL == pfr) { perror("open file test1.txt"); } //不斷的從原始檔中讀取字元並寫入目的檔案中,直到遇到EOF結束這個過程 while ((ch=fgetc(pfr))!=EOF)//EOF是檔案結束標誌 { fputc(ch,pfw); } //關閉流,使用完流後記得關閉,避免佔用資源 fclose(pfr); fclose(pfw); pfr = NULL; pfw = NULL; return 0; }