一、实验目的
二、实验准备
三、实验内容
四、实验结论
1. 实验任务4
源代码:
1 #include<stdio.h> 2 #include<string.h> 3 #define N 1000 4 5 int main(){ 6 FILE *fp; 7 8 fp = fopen("data4.txt","r"); 9 10 if(fp == NULL){ 11 printf("open file fail\n"); 12 return 1; 13 } 14 15 char x[N]; 16 int s = 0; 17 18 while(!feof(fp)){ 19 fscanf(fp,"\n%c",&x[s]); 20 s++; 21 } 22 23 int i,t = 0; 24 for(i = 0; i < s; i++){ 25 if(x[i]) 26 t++; 27 else 28 continue; 29 } 30 31 printf("%d",t); 32 fclose(fp); 33 return 0; 34 } 35
运行结果:
2. 实验任务5
源代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #define N 10 6 7 typedef struct { 8 long int id; 9 char name[20]; 10 float objective; // 客观题得分 11 float subjective; // 操作题得分 12 float sum; // 总分 13 char ans[10]; // 考试结果 14 } STU; 15 16 // 函数声明 17 void finput(STU st[], int n); 18 void foutput(STU st[], int n); 19 void output(STU st[], int n); 20 int process(STU st[], int n, STU st_pass[]); 21 22 int main() { 23 STU stu[N], stu_pass[N]; 24 int cnt; 25 double pass_rate; 26 27 printf("从文件读入%d个考生信息...\n", N); 28 finput(stu, N); 29 30 printf("\n对考生成绩进行统计...\n"); 31 cnt = process(stu, N, stu_pass); 32 33 printf("\n通过考试的名单:\n"); 34 output(stu,N); // 输出到屏幕 35 foutput(stu, N); // 输出到文件 36 37 pass_rate = 1.0 * cnt / N; 38 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 39 40 return 0; 41 } 42 43 // 把通过考试的考生完整信息输出到屏幕上 44 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 45 void output(STU st[], int n) { 46 int i; 47 48 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 49 for (i = 0; i < n; i++) 50 printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].ans); 51 } 52 53 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 54 void finput(STU st[], int n) { 55 int i; 56 FILE *fin; 57 58 fin = fopen("examinee.txt", "r"); 59 if (fin == NULL) { 60 printf("fail to open file\n"); 61 exit(0); 62 } 63 64 while (!feof(fin)) { 65 for (i = 0; i < n; i++) 66 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 67 } 68 69 fclose(fin); 70 } 71 72 // 把通过考试的考生完整信息写入文件list_pass.txt 73 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 74 void foutput(STU s[], int n) { 75 FILE *fout; 76 int i; 77 78 // 保存到文件 79 fout = fopen("list_pass.txt", "w"); 80 if (!fout) { 81 printf("fail to open or create list_pass.txt\n"); 82 exit(0); 83 } 84 85 fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 86 87 for (i = 0; i < n; i++) 88 fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].ans); 89 90 fclose(fout); 91 } 92 93 94 95 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 96 int process(STU st[], int n, STU st_pass[]) { 97 int i,t = 0; 98 for(i = 0; i < n; i++){ 99 st[i].sum = st[i].objective + st[i].subjective; 100 if(st[i].sum >= 60){ 101 st_pass[t++] = st[i]; 102 strcpy(st[i].ans,"pass"); 103 } 104 else 105 strcpy(st[i].ans,"fail"); 106 } 107 108 return t; 109 }
运行结果:
文件内容:
3. 实验任务6
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 int main(){ 6 char x[80][500]; 7 8 FILE *fp; 9 fp = fopen("list.txt","r"); 10 if(fp == NULL){ 11 printf("file open fail"); 12 return 1; 13 } 14 15 int i = 0; 16 while(!feof(fp)){ 17 fgets(x[i],500,fp); 18 i++; 19 } 20 21 fclose(fp); 22 23 FILE *f; 24 f = fopen("lucky.txt","w"); 25 if(f == NULL){ 26 printf("file create fail"); 27 return 1; 28 } 29 30 int t; 31 srand(time(NULL)); 32 for(i = 1; i <= 5; i++){ 33 t = rand()%80 + 1; 34 printf("%s",x[t]); 35 fprintf(f,"%s",x[t]); 36 } 37 38 fclose(f); 39 40 return 0; 41 }
运行结果:
文件内容:
选做:
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <string.h> 5 6 typedef struct{ 7 int id; 8 char name[20]; 9 char class_name[100]; 10 }STU; 11 int main(){ 12 STU x[80]; 13 14 FILE *fp; 15 fp = fopen("list.txt","r"); 16 if(fp == NULL){ 17 printf("file open fail"); 18 return 1; 19 } 20 21 int i = 0; 22 while(!feof(fp)){ 23 fscanf(fp,"%d%s%s",&x[i].id,x[i].name,x[i].class_name); 24 i++; 25 } 26 27 fclose(fp); 28 29 30 FILE *f; 31 time_t data = time(NULL); 32 struct tm *localTime = localtime(&data); 33 34 char d[80]; 35 strftime(d,sizeof(d),"%Y-%m-%d",localTime); 36 strcat(d,".txt"); 37 38 f = fopen(d,"w"); 39 if(f == NULL){ 40 printf("file create fail"); 41 return 1; 42 } 43 44 int t; 45 int y[80] = {0}; 46 STU s[5]; 47 48 srand(time(NULL)); 49 for(i = 0; i < 5; i++){ 50 t = rand()%80; 51 if(y[t] == 1) 52 continue; 53 s[i] = x[t]; 54 y[t] = 1; 55 } 56 57 int j; 58 STU z; 59 for(i = 0; i < 4; i++){ 60 for(j = 0; j < 4 - i; j++){ 61 if(s[j].id > s[j + 1].id){ 62 z = s[j]; 63 s[j] = s[j + 1]; 64 s[j + 1] = z; 65 } 66 } 67 } 68 69 for(i = 0; i < 5; i++){ 70 printf("%-10d%10s%20s\n",s[i].id,s[i].name,s[i].class_name); 71 fprintf(f,"%-10d%10s%20s\n",s[i].id,s[i].name,s[i].class_name); 72 } 73 74 fclose(f); 75 76 return 0; 77 }
运行结果:
文件内容:
五、实验总结
1.重新捡回了生成随机数的知识,收获了让文件名附为当前时间的办法,强化了上网搜索信息学习新知的能力
2.学会了如何通过scanf("\n%c")来跳过空白符读取
3.实验6写必做部分时的报错,加深了对scanf("%s")在读取中遇空白符即停止的特点(实验4中我的舍友就是运用这一点循环读入数组直到文件结束来跳过空白符)
标签:fp,文件,int,编程,++,st,STU,实验,printf From: https://www.cnblogs.com/forgiver/p/17910117.html