# C語言題庫-086 有兩個磁盤文件A和B, 各存放一行字母,要求把 這兩個文件中的信息合併(按字母順序排列),輸出 到一個新文件C 中。 【參考答案】 ``` main() { FILE *fp; in t i,j,n,ni; char c[160],t,ch; if((fp=fopen(“A”,“r “))==NULL) { printf(“file A cannot be opened\n”);exit(0); } printf(”\nA contents are :\n”); for(i=0;(ch=fgetc(fp))!=EOF ; i++) { c[i]=ch;pu tchar(c[i]); } fclose(fp); ni=i; if((fp=fopen(“B”,“r “))==NULL) { printf(“file B cannot be opened\n”);exit(0); } printf(”\nB contents are :\n”); for(i=0;(ch=fgetc(fp))!=EOF ; i++) { c[i]=ch;pu tchar(c[i]); } fclose(fp); n=i; for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(c[i]>c[j]){t=c[i ];c[i]=c[j];c[j]=t;} printf("\nC file is:\n"); fp =f open(“C”,“w”); for(i=0;i<n;i++){putc(c[i ],fp);putchar(c[i]);} fclose(fp); getch(); } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up