试验七
partq1 驗(yàn)證性試驗(yàn)
// 從文本文件file1.dat中讀取數(shù)據(jù),找出最高分和最低分學(xué)生信息,并輸出在屏幕上 #include <stdio.h> #include <stdlib.h>#define N 10// 定義一個(gè)結(jié)構(gòu)體類型STU typedef struct student {int num;char name[20];int score; }STU;int main() {STU st, stmax, stmin;int i;FILE *fp;// 以只讀文本方式打開文件file1.dat fp = fopen("file1.dat", "r");if( !fp ) { // 如果打開失敗,則輸出錯(cuò)誤提示信息,然后退出程序 printf("fail to open file1.dat\n");exit(0);}stmax.score = 0; // 先假定最高分是0,后面如發(fā)現(xiàn)比當(dāng)前最高分還高的分?jǐn)?shù),就更新最高分 stmin.score = 100; // 先假定最低分是100分,后面如發(fā)現(xiàn)比當(dāng)前最低分更低的分?jǐn)?shù),就更新最低分 while( !feof(fp)) {fscanf(fp, "%d %s %d", &st.num, st.name, &st.score); // 從fp指定的文件中格式化讀取一個(gè)學(xué)生信息if(st.score > stmax.score)stmax = st;else if(st.score < stmin.score)stmin = st; } fclose(fp);printf("最高分學(xué)生信息: %5d%15s%5d\n", stmax.num, stmax.name, stmax.score);printf("最低分學(xué)生信息: %5d%15s%5d\n", stmin.num, stmin.name, stmin.score);return 0; }// 這是《C語言程序設(shè)計(jì)教程學(xué)習(xí)指導(dǎo)》「2.10 文件」中的實(shí)驗(yàn),細(xì)微處做了微調(diào) // 這個(gè)源代碼沒有考慮多個(gè)高分或多個(gè)低分的情形。對比驗(yàn)證性實(shí)驗(yàn)3和驗(yàn)證性實(shí)驗(yàn)4的程序源碼及運(yùn)行結(jié)果,總結(jié)比較二進(jìn)制文件與文本文件的區(qū)別:主要區(qū)別是存儲數(shù)值型數(shù)據(jù)的形式不同
文本文件:數(shù)據(jù)流由一個(gè)個(gè)字符組成,每個(gè)字符均以ASCII代碼存儲,占一個(gè)字節(jié)
二進(jìn)制文件:數(shù)據(jù)流由二進(jìn)制字節(jié)代碼組成,將數(shù)據(jù)按其在內(nèi)存中的存儲形式存儲在文件中
3寫一個(gè)簡單的程序,嘗試從二進(jìn)制文件?le4.dat中讀出數(shù)據(jù),并在屏幕上顯示,以此查看文件?le4.dat的內(nèi) 容。
// 從文本數(shù)據(jù)文件file1.dat中讀入數(shù)據(jù),按成績從高到低排序,將排序結(jié)果輸出到屏幕上,同時(shí)以文本方式存入文件file3.dat中。 #include <stdio.h> #include <stdlib.h>#define N 10// 定義一個(gè)結(jié)構(gòu)體類型STU typedef struct student {int num;char name[20];int score; }STU;void sort(STU *pst, int n); // 函數(shù)聲明int main() {FILE *fin, *fout;STU st[N];int i;// 以只讀文本方式打開文件file1.datfin = fopen("file1.dat", "r");if( !fin ) { // 如果打開失敗,則輸出錯(cuò)誤提示信息,然后退出程序printf("fail to open file1.dat\n");exit(0);}// 從fin指向的數(shù)據(jù)文件file1.dat中讀取數(shù)據(jù)到結(jié)構(gòu)體數(shù)組stfor(i=0; i<N; i++)fscanf(fin, "%d %s %d", &st[i].num, st[i].name, &st[i].score);fclose(fin); // 關(guān)閉fin指向的文件file1.dat// 調(diào)用函數(shù)sort()對數(shù)組st中數(shù)據(jù),按分?jǐn)?shù)又高到低排序 sort(st, N);// 以寫方式打開/創(chuàng)建文本文件file3.datfout = fopen("file3.dat", "w");if( !fout ) { // 如果打開失敗,則輸出錯(cuò)誤提示信息,然后退出程序printf("fail to open file1.dat\n");exit(0);}// 將排序后的數(shù)組st中數(shù)據(jù)輸出到屏幕,同時(shí),也寫入文件file3.datfor(i=0; i<N; i++) {printf("%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score);fprintf(fout, "%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score);}fclose(fout); // 關(guān)閉fout指向的文件file3.datreturn 0; }// 函數(shù)功能描述:對pst指向的n個(gè)STU結(jié)構(gòu)體數(shù)據(jù)進(jìn)行排序,按成績數(shù)據(jù)項(xiàng)由高到底排序 // 排序算法:冒泡法 void sort(STU *pst, int n) {STU *pi, *pj, t;for(pi = pst; pi < pst+n-1; pi++)for(pj = pi+1; pj < pst+n; pj++)if(pi->score < pj->score) {t = *pi;*pi = *pj;*pj = t;}}part2
// 從文本數(shù)據(jù)文件file1.dat中讀入數(shù)據(jù),按成績從高到低排序,將排序結(jié)果輸出到屏幕上,同時(shí)以文本方式存入文件file3.dat中。 #include <stdio.h> #include <stdlib.h>#define N 10// 定義一個(gè)結(jié)構(gòu)體類型STU typedef struct student {int num;char name[20];int score; }STU;void sort(STU *pst, int n); // 函數(shù)聲明int main() {FILE *fin, *fout;STU st[N];int i;// 以只讀文本方式打開文件file1.datfin = fopen("file1.dat", "r");if( !fin ) { // 如果打開失敗,則輸出錯(cuò)誤提示信息,然后退出程序printf("fail to open file1.dat\n");exit(0);}// 從fin指向的數(shù)據(jù)文件file1.dat中讀取數(shù)據(jù)到結(jié)構(gòu)體數(shù)組stfor(i=0; i<N; i++)fscanf(fin, "%d %s %d", &st[i].num, st[i].name, &st[i].score);fclose(fin); // 關(guān)閉fin指向的文件file1.dat// 調(diào)用函數(shù)sort()對數(shù)組st中數(shù)據(jù),按分?jǐn)?shù)又高到低排序 sort(st, N);// 以寫方式打開/創(chuàng)建文本文件file3.datfout = fopen("file3.dat", "w");if( !fout ) { // 如果打開失敗,則輸出錯(cuò)誤提示信息,然后退出程序printf("fail to open file1.dat\n");exit(0);}// 將排序后的數(shù)組st中數(shù)據(jù)輸出到屏幕,同時(shí),也寫入文件file3.datfor(i=0; i<N; i++) {printf("%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score);fprintf(fout, "%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score);}fclose(fout); // 關(guān)閉fout指向的文件file3.datreturn 0; }// 函數(shù)功能描述:對pst指向的n個(gè)STU結(jié)構(gòu)體數(shù)據(jù)進(jìn)行排序,按成績數(shù)據(jù)項(xiàng)由高到底排序 // 排序算法:冒泡法 void sort(STU *pst, int n) {STU *pi, *pj, t;for(pi = pst; pi < pst+n-1; pi++)for(pj = pi+1; pj < pst+n; pj++)if(pi->score < pj->score) {t = *pi;*pi = *pj;*pj = t;}}總結(jié)最后一次實(shí)驗(yàn)結(jié)束了 想一想自己這一路學(xué)的很多 。也謝謝老師。
轉(zhuǎn)載于:https://www.cnblogs.com/hsc6/p/11080530.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
- 上一篇: ThinkPHP模板之二
- 下一篇: taglib遍历foreach循环lis