Part1: 驗證性實驗html
1 // 將file1.txt中小寫字母轉換成大寫後,另存爲file2.txt 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 int main() { 6 FILE *fin, *fout; // 定義文件類型指針 7 int ch; 8 9 fin = fopen("file1.txt", "r"); // 以只讀文本方式打開文件file1.txt 10 if (fin == NULL) { 11 printf("fail to open file1.txt\n"); 12 exit(0); 13 } 14 15 fout = fopen("d:\\file2.txt", "w"); // 以寫文本方式打開文件file2.txt, 若是文件不存在,就建立一個 16 if (fout == NULL) { 17 printf("fail to open or create file2.txt\n"); 18 exit(0); 19 } 20 21 while( !feof(fin) ) { 22 ch = fgetc(fin); // 從fin指向的文件file1.txt中讀取單個字符,暫存在字符變量ch中 23 24 if(ch >= 'a' && ch <= 'z') // 若是是小寫字母,則轉換成大寫 25 ch -= 32; 26 27 fputc(ch, fout); // 將字符變量ch中的字符寫入fout指向的文件file2.txt中 28 } 29 30 fclose(fin); 31 fclose(fout); 32 33 return 0; 34 }
觀察file3與file4能夠找到端倪,可是二進制存儲方式不可直接可讀。
編程
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define N 10 5 6 typedef struct student { 7 int num; 8 char name[20]; 9 int score; 10 }STU; 11 12 int main() { 13 FILE *fin; 14 int i; 15 STU st[N]; 16 17 fin = fopen("file4.dat", "rb"); // 以只讀文本方式打開文件file4.txt 18 if (fin == NULL) { 19 printf("fail to open file\n"); 20 exit(0); 21 } 22 while(!feof(fin)){ 23 fread(&st[i],sizeof(struct student),1,fin); 24 printf("%-6d %-10s %3d\n", st[i].num, st[i].name, st[i].score); 25 i++; 26 } 27 fclose(fin); 28 29 30 }
代碼最後一行出現一串奇怪的數字?這是爲何呢?數組
Part2: 編程練習ide
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 const int N = 10; 5 6 // 定義結構體類型struct student,並定義其別名爲STU 7 typedef struct student { 8 long int id; 9 char name[20]; 10 float objective; /*客觀題得分*/ 11 float subjective; /*操做題得分*/ 12 float sum; 13 char level[10]; 14 }STU; 15 16 // 函數聲明 17 void input(STU s[], int n); 18 void output(STU s[], int n); 19 void process(STU s[], int n); 20 21 int main() { 22 STU stu[N]; 23 24 printf("錄入%d個考生信息: 准考證號,姓名,客觀題得分(<=40),操做題得分(<=60)\n", N); 25 input(stu, N); 26 27 printf("\n對考生信息進行處理: 計算總分,肯定等級\n"); 28 process(stu, N); 29 30 printf("\n打印考生完整信息: 准考證號,姓名,客觀題得分,操做題得分,總分,等級\n"); 31 output(stu, N); 32 33 return 0; 34 } 35 36 // 從文本文件examinee.txt讀入考生信息:准考證號,姓名,客觀題得分,操做題得分 37 void input(STU s[], int n) { 38 FILE *fp; 39 fp = fopen("examinee.txt", "r"); 40 if( !fp ) { // 若是打開失敗,則輸出錯誤提示信息,而後退出程序 41 printf("fail to open examinee.txt\n"); 42 exit (0); 43 } 44 int i; 45 for(i=0;i<n;i++){ 46 //從fp指定的文件中格式化讀取學生信息 47 if(fscanf(fp,"%ld %s %f %f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective)==0) 48 printf("讀取錯誤!"); 49 50 } 51 fclose(fp); //關閉文件是個好習慣 52 } 53 54 // 輸出考生完整信息: 准考證號,姓名,客觀題得分,操做題得分,總分,等級 55 // 不只輸出到屏幕上,還寫到文本文件result.txt中 56 void output(STU s[], int n) { 57 int i; 58 FILE *fout; 59 //只以寫文本方式打開 60 fout = fopen("result.txt", "w"); 61 62 63 //在屏幕和文件中打印表頭 64 printf("准考證號 姓名 客觀題得分 操做題得分 總分 等級 \n "); 65 fprintf(fout,"准考證號 姓名 客觀題得分 操做題得分 總分 等級\n"); 66 67 for(i=0;i<n;i++){ //使用循環輸出數據 68 printf("%-9ld%-10s%-12.2f%-12.2f%-10.2f%-8s\n", 69 s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); 70 fprintf(fout,"%-9ld%-10s%-1.2f%-12.2f%-10.2f%-8s\n", 71 s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); 72 } 73 fclose(fout); 74 } 75 76 // 對考生信息進行處理:計算總分,排序,肯定等級 77 void process(STU s[], int n) { 78 int i,j; 79 int high=0,mid=0; 80 STU temp; 81 82 for(i=0;i<n;i++){ 83 s[i].sum=s[i].objective + s[i].subjective; 84 } 85 for(i=0;i<n-1;i++){ //使用冒泡排序法對成績作排名 86 for(j=0;j<n-1-i;j++) 87 if(s[j].sum<s[j+1].sum){ 88 temp=s[j]; 89 s[j]=s[j+1]; 90 s[j+1]=temp; 91 } 92 93 } 94 high=(int)n*0.1;mid=(int)n*0.5;//進行等級劃分 95 96 for(i=0;i<high-1;i++) //由於數組第一個數字是0 97 strcpy(s[i].level,"優秀"); 98 99 for(i=high-1;i<mid-1;i++) 100 strcpy(s[i].level,"合格"); 101 102 for(i=mid-1;i<n;i++) 103 strcpy(s[i].level,"不合格"); 104 }
直接在d盤中建立file2,老師給的源代碼多是修改後未更正,致使我在第一次試驗中的原路徑裏未找到文件。函數
實驗總結:spa
https://www.cnblogs.com/zys-0119/p/11046073.html#42839653d
https://www.cnblogs.com/weiyuyang/p/11046961.html#4283960指針
https://www.cnblogs.com/aoliaoliao/p/11050192.html#4283957code