C語言逐行讀取檔案內容,寫入另外一個檔案
阿新 • • 發佈:2019-02-10
<span style="font-family:Microsoft YaHei;font-size:14px;">#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { FILE * fp; FILE * fd; char buf[1024]; fp=fopen("data1.txt","r"); if(fp==NULL) { perror("open file"); exit(0); } fd=fopen("data2.txt","w"); if(fd==NULL) { perror("open file"); exit(0); } while(fgets(buf,sizeof(buf),fp)!=NULL) { fputs(buf,fd); printf("%s", buf); } fclose(fd); fclose(fp); return 0; }</span>