C 實戰練習題目99

題目:有兩個磁盤文件A和B,各存放一行字母,要求把這兩個文件中的信息合併(按字母順序排列),輸出到一個新文件C中。html

程序分析:你須要先建立 A.txt 與 B.txt。學習

A.txt文件內容:spa

123

B.txt文件內容:code

456

程序源代碼:htm

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 int main()
 5 {
 6     FILE*fa,*fb,*fc;
 7     int i,j,k;
 8     char str[100],str1[100];
 9     char tem;
10     if((fa=fopen("A.txt","r"))==NULL) // A.txt 文件須要存在
11     {
12         printf("error: cannot open A file!\n");
13         exit(0);
14     }
15     fgets(str,99,fa);
16     fclose(fa);
17     if((fb=fopen("B.txt","r"))==NULL)  // B.txt 文件須要存在
18     {
19         printf("error: cannot open B file!\n");
20         exit(0);
21     }
22     fgets(str1,100,fb);
23     fclose(fb);
24     strcat(str,str1);
25     for(i=strlen(str)-1;i>1;i--)
26         for(j=0;j<i;j++)
27             if(str[j]>str[j+1])
28             {
29                 tem=str[j];
30                 str[j]=str[j+1];
31                 str[j+1]=tem;
32             }
33     
34     if((fc=fopen("C.txt","w"))==NULL)  // 合併爲 C.txt
35     {
36         printf("error: cannot open C file!\n");
37         exit(0);
38     }
39     fputs(str,fc);
40     fclose(fc);
41     system("pause");
42     return 0;
43 }

以上實例運行輸出結果後,打開 C.txt 內容以下:blog

123456

感謝你的閱讀,請用心感悟!但願能夠幫到愛學習的你!!分享也是一種快樂!!!請接力。。。get

點擊查看原文,謝謝!string

相關文章
相關標籤/搜索