实验7
任务1
源代码
1 #include <stdio.h> 2 #define N 80 3 #define M 100 4 5 typedef struct { 6 char name[N]; // 书名 7 char author[N]; // 作者 8 } Book; 9 10 void write(); 11 void read(); 12 13 int main() { 14 printf("测试1:把图书信息写入文本文件\n"); 15 write(); 16 17 printf("\n测试2:从文本文件读取图书信息,打印输出到屏幕\n"); 18 read(); 19 20 return 0; 21 } 22 23 void write() { 24 Book x[] = { 25 {"《雕塑家》", "斯科特.麦克劳德"}, 26 {"《灯塔》", "克里斯多夫.夏布特"}, 27 {"《人的局限性》", "塞缪尔.约翰生"}, 28 {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"}, 29 {"《大地之上》", "罗欣顿.米斯特里"}, 30 {"《上学记》", "何兆武"}, 31 {"《命运》", "蔡崇达"} 32 }; 33 int n, i; 34 FILE *fp; 35 36 // 计算数组x中元素个数 37 n = sizeof(x) / sizeof(x[0]); 38 39 // 以写的方式打开文本文件data1.txt 40 fp = fopen("data1.txt", "w"); 41 // 如果打开文件失败,输出提示信息并返回 42 if (fp == NULL) { 43 printf("fail to open file to write\n"); 44 return; 45 } 46 47 // 将结构体数组x中的图书信息格式化写到fp指向的文件data1.txt 48 for(i = 0; i < n; ++i) 49 fprintf(fp, "%-40s %-20s\n", x[i].name, x[i].author); 50 51 fclose(fp); 52 } 53 54 void read() { 55 Book x[M]; 56 int i, n; 57 FILE *fp; 58 59 // 以读的方式打开文本文件data1.txt 60 fp = fopen("data1.txt", "r"); 61 // 如果打开文件失败,输出提示信息并返回 62 if (fp == NULL) { 63 printf("fail to open file to read\n"); 64 return; 65 } 66 67 // 从文件中读取图书信息,保存到结构体数组x中 68 i = 0; 69 while(fscanf(fp, "%s%s", x[i].name, x[i].author) != EOF) 70 ++i; 71 72 // 将图书信息打印输出到屏幕上 73 n = i; 74 for(i = 0; i < n; ++i) 75 printf("%d. %-40s %-20s\n", i+1, x[i].name, x[i].author); 76 77 fclose(fp); 78 }
截图

任务2
源代码
1 #include <stdio.h> 2 #include <stdlib.h> // 用于system("pause")防止闪退 3 #define N 80 4 #define M 100 5 6 typedef struct { 7 char name[N]; // 书名 8 char author[N]; // 作者 9 } Book; 10 11 void write(); 12 void read(); 13 14 int main() { 15 printf("测试1:把图书信息以数据块方式写入二进制文件\n"); 16 write(); 17 18 printf("\n测试2:从二进制文件读取图书信息,打印输出到屏幕\n"); 19 read(); 20 21 system("pause"); // 防止控制台运行完闪退 22 return 0; 23 } 24 25 void write() { 26 Book x[] = { 27 {"《雕塑家》", "斯科特.麦克劳德"}, 28 {"《灯塔》", "克里斯多夫.夏布特"}, 29 {"《人的局限性》", "塞缪尔.约翰生"}, 30 {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"}, 31 {"《大地之上》", "罗欣顿.米斯特里"}, 32 {"《上学记》", "何兆武"}, 33 {"《命运》", "蔡崇达"} 34 }; 35 int n, i; 36 FILE *fp; 37 38 // 计算数组x中元素个数 39 n = sizeof(x) / sizeof(x[0]); 40 41 // 以写的方式打开二进制文件data2.dat,wb=write binary 42 fp = fopen("data2.dat", "wb"); 43 // 如果打开文件失败,输出提示信息并返回 44 if (fp == NULL) { 45 printf("fail to open file to write\n"); 46 return; 47 } 48 49 // 将结构体数组整块写入二进制文件 50 fwrite(x, sizeof(Book), n, fp); 51 52 fclose(fp); 53 } 54 55 void read() { 56 Book x[M]; 57 int i, n; 58 FILE *fp; 59 60 // 以读的方式打开二进制文件data2.dat,rb=read binary 61 fp = fopen("data2.dat", "rb"); 62 // 如果打开文件失败,输出提示信息并返回 63 if (fp == NULL) { 64 printf("fail to open file to read\n"); 65 return; 66 } 67 68 // 循环单块读取结构体,读到文件末尾停止 69 i = 0; 70 while(fread(&x[i], sizeof(Book), 1, fp) == 1) 71 ++i; 72 73 // 在屏幕上打印输出全部图书 74 n = i; 75 for(i = 0; i < n; ++i) 76 printf("%d. %-40s %-20s\n", i+1, x[i].name, x[i].author); 77 78 fclose(fp); 79 }
截图

任务3
源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define N 100 4 #define M 80 5 6 void write(); 7 void read_str(); 8 void read_char(); 9 10 int main() { 11 printf("测试1:把一组字符信息以字符串方式写入文本文件\n"); 12 write(); 13 14 printf("\n测试2:从文件以字符串方式读取,输出到屏幕\n"); 15 read_str(); 16 17 printf("\n测试3:从文件以单个字符方式读取,输出到屏幕\n"); 18 read_char(); 19 20 system("pause"); 21 return 0; 22 } 23 24 void write() { 25 char *ptr[] = { 26 "Working's Blues", 27 "Everything Will Flow", 28 "Streets of London", 29 "Perfect Day", 30 "Philadelphia" 31 }; 32 int i, n; 33 FILE *fp; 34 35 fp = fopen("data3.txt", "w"); 36 if(fp == NULL) { 37 printf("fail to open file to write\n"); 38 return; 39 } 40 41 n = sizeof(ptr)/sizeof(ptr[0]); 42 for(i = 0; i < n; ++i) { 43 fputs(ptr[i], fp); 44 fputs("\n", fp); 45 } 46 fclose(fp); 47 } 48 49 void read_str() { 50 char songs[N][M]; 51 int i, n; 52 FILE *fp; 53 54 fp = fopen("data3.txt", "r"); 55 if(fp == NULL) { 56 printf("fail to open file to read\n"); 57 return; 58 } 59 60 i = 0; 61 while(i < N && (fgets(songs[i], M, fp) != NULL)) 62 ++i; 63 n = i; 64 65 for(i = 0; i < n; ++i) 66 printf("%d. %s", i+1, songs[i]); 67 fclose(fp); 68 } 69 70 void read_char() { 71 int ch; 72 FILE *fp; 73 74 fp = fopen("data3.txt", "r"); 75 if(fp == NULL) { 76 printf("fail to open file to read\n"); 77 return; 78 } 79 80 while((ch = fgetc(fp)) != EOF) 81 putchar(ch); 82 fclose(fp); 83 }
截图

思考
\是转义字符起始符,不会作为普通自负打印
i<N是数组越界防护条件
任务4
源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 FILE *fp; 6 fp = fopen("data4.txt","r"); 7 if(fp == NULL) 8 { 9 printf("打开文件失败\n"); 10 return 1; 11 } 12 int ch; 13 int hang = 0; 14 int zifu = 0; 15 ch = fgetc(fp); 16 while(ch != EOF) 17 { 18 if(ch == ' ') 19 { 20 ch = fgetc(fp); 21 continue; 22 } 23 if(ch == '\t') 24 { 25 ch = fgetc(fp); 26 continue; 27 } 28 if(ch == '\n') 29 { 30 hang = hang + 1; 31 ch = fgetc(fp); 32 continue; 33 } 34 zifu = zifu + 1; 35 ch = fgetc(fp); 36 } 37 fclose(fp); 38 printf("data4.txt统计结果:\n"); 39 printf("行数:\t\t%d\n",hang); 40 printf("字符数(不计空白符):\t%d\n",zifu); 41 system("pause"); 42 return 0; 43 }
截图

任务5
#include <stdio.h> #include <string.h> #define N 10 typedef struct { long id; char name[20]; float objective; float subjective; float sum; char result[10]; } STU; void read(STU st[], int n); void write(STU st[], int n); void output(STU st[], int n); int process(STU st[], int n, STU st_pass[]); int main() { STU stu[N], stu_pass[N]; int cnt; double pass_rate; printf("从文件读入%d个考生信息:已完成\n", N); read(stu, N); printf("\n对考生成绩进行统计:已完成\n"); cnt = process(stu, N, stu_pass); printf("\n所有考生完整信息:\n"); output(stu, N); printf("\n通过考试的名单写入文件:已完成!\n"); write(stu_pass, cnt); pass_rate = 1.0 * cnt / N; printf("\n本次等级考试通过率:%.2f%%\n", pass_rate*100); return 0; } void output(STU st[], int n) { int i; printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n"); for (i = 0; i < n; i++) printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); } void read(STU st[], int n) { int i; FILE *fin; fin = fopen("examinee.txt", "r"); if (!fin) { printf("fail to open file\n"); return; } for (i = 0; i < n; i++) fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); fclose(fin); } int process(STU st[], int n, STU st_pass[]) { int i, k = 0; for (i = 0; i < n; i++) { st[i].sum = st[i].objective + st[i].subjective; if (st[i].sum >= 60) { strcpy(st[i].result, "通过"); st_pass[k] = st[i]; k = k + 1; } else { strcpy(st[i].result, "未通过"); } } return k; } void write(STU st[], int n) { int i; FILE *fout; fout = fopen("list_pass.txt", "w"); if (!fout) { printf("fail to open file\n"); return; } fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n"); for (i = 0; i < n; i++) fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); fclose(fout); }
截图

任务6
源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <string.h> 5 #define MAX 80 6 7 typedef struct{ 8 char id[20]; 9 char name[20]; 10 char cls[30]; 11 }Stu; 12 13 int main() 14 { 15 Stu all[MAX]; 16 Stu win[5]; 17 int i,j,num; 18 int flag[MAX]={0}; 19 char filename[30]; 20 FILE *fp; 21 22 fp=fopen("list.txt","r"); 23 if(fp==NULL) 24 { 25 printf("打开文件失败\n"); 26 return 1; 27 } 28 num=0; 29 while(fscanf(fp,"%s %s %s",all[num].id,all[num].name,all[num].cls)!=EOF) 30 { 31 num++; 32 } 33 fclose(fp); 34 35 srand((unsigned)time(NULL)); 36 i=0; 37 while(i<5) 38 { 39 int r=rand()%80; 40 if(flag[r]==0) 41 { 42 flag[r]=1; 43 win[i]=all[r]; 44 i++; 45 } 46 } 47 48 printf("--------中奖名单--------\n"); 49 for(i=0;i<5;i++) 50 { 51 printf("%s %s %s\n",win[i].id,win[i].name,win[i].cls); 52 } 53 54 printf("请输入保存的文件名:"); 55 scanf("%s",filename); 56 fp=fopen(filename,"w"); 57 for(i=0;i<5;i++) 58 { 59 fprintf(fp,"%s %s %s\n",win[i].id,win[i].name,win[i].cls); 60 } 61 fclose(fp); 62 printf("------保存到文件------\n"); 63 system("pause"); 64 return 0; 65 }
截图


浙公网安备 33010602011771号