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


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


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


问题
1)\’是转义字符,表示输出'
2)i<N的作用是防止数组越界
实验任务4
源代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 4 void write(); 5 void read_line_chars(); 6 7 int main() { 8 printf("data4.txt统计结果:\n"); 9 write(); 10 read_line_chars(); 11 12 return 0; 13 } 14 15 void write() { 16 char* ptr[] = { "0123456789-0123456789", 17 "nuist2025", 18 "cosmos galaxy" }; 19 int i, n; 20 FILE* fp; 21 22 fp = fopen("data4.txt", "w"); 23 if (fp == NULL) { 24 printf("fail to open file to write\n"); 25 return; 26 } 27 n = sizeof(ptr) / sizeof(ptr[0]); 28 29 for (i = 0; i < n; ++i) { 30 fputs(ptr[i], fp); 31 fputs("\n", fp); 32 } 33 34 fclose(fp); 35 } 36 37 void read_line_chars() { 38 int ch; 39 int line = 0; 40 int cha = 0; 41 FILE* fp; 42 43 fp = fopen("data4.txt", "r"); 44 if (fp == NULL) { 45 printf("fail to open file to read\n"); 46 return; 47 } 48 49 while ((ch = fgetc(fp)) != EOF) { 50 if (ch == '\n') { 51 line++; 52 } 53 54 if (ch != ' ' && ch != '\t' && ch != '\r' && ch != '\n') { 55 cha++; 56 } 57 58 } 59 60 printf("行数: %d\n", line); 61 printf("字符数(不计空白符):%d\n", cha); 62 fclose(fp); 63 }
运行结果


实验任务5
源代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <string.h> 4 5 #define N 10 6 7 typedef struct { 8 long id; 9 char name[20]; 10 float objective; 11 float subjective; 12 float sum; 13 char result[10]; 14 }STU; 15 16 void read(STU st[], int n); 17 void write(STU st[], int n); 18 void output(STU st[], int n); 19 int process(STU st[], int n, STU st_pass[]); 20 21 int main() { 22 STU stu[N], stu_pass[N]; 23 int cnt; 24 double pass_rate; 25 26 printf("\n从文件读入%d个考生信息...\n", N); 27 read(stu, N); 28 29 printf("\n对考生成绩进行统计...\n"); 30 cnt = process(stu, N, stu_pass); 31 32 printf("\n通过考生的名单:\n"); 33 output(stu, N); 34 write(stu_pass,cnt); 35 36 pass_rate = 1.0 * cnt / N; 37 printf("\n本次考试通过率:%.2f%%\n", pass_rate * 100); 38 39 return 0; 40 } 41 42 void output(STU st[], int n) { 43 int i; 44 45 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 46 for (i = 0; i < n; i++) 47 printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name, 48 st[i].objective, st[i].subjective, st[i].sum, st[i].result); 49 } 50 51 void read(STU st[], int n) { 52 int i; 53 FILE* fin; 54 55 fin = fopen("examinee.txt", "r"); 56 if (!fin) { 57 printf("fail to open file\n"); 58 return; 59 } 60 61 for(i=0;i<n;i++) 62 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, 63 &st[i].subjective); 64 65 fclose(fin); 66 } 67 68 void write(STU st[], int n) { 69 int i; 70 FILE* fp; 71 72 fp = fopen("list_pass.txt", "w"); 73 if (!fp) { 74 printf("fail to open file to write\n"); 75 return; 76 } 77 78 fprintf(fp, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 79 for (i = 0; i < n; ++i) { 80 fprintf(fp,"%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name, 81 st[i].objective, st[i].subjective, st[i].sum, st[i].result); 82 } 83 fclose(fp); 84 } 85 86 int process(STU st[], int n, STU st_pass[]) { 87 int i; 88 int pass = 0; 89 90 for (i = 0; i < n; ++i) { 91 st[i].sum = st[i].objective + st[i].subjective; 92 if (st[i].sum >= 60) { 93 strcpy(st[i].result, "通过"); 94 st_pass[pass++] = st[i]; 95 } 96 else { 97 strcpy(st[i].result, "未通过"); 98 } 99 } 100 101 return pass; 102 }
运行结果

一直在烫烫烫QAQ
实验任务6
源代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <time.h> 5 #include <string.h> 6 #define N 100 7 #define M 5 8 9 int main() { 10 char s[N][N]; 11 int has_hit[N] = { 0 }; 12 char hit[N][N]; 13 FILE* fp, * fout; 14 int i, n; 15 char filename[80]; 16 int lucky_i; 17 char tmp[80]; 18 19 fp = fopen("C:\\Users\\asus\\Desktop\\新建文件夹 (2)\\list.txt", "r"); 20 if (!fp) { 21 perror("list.txt"); 22 return 1; 23 } 24 25 i = 0; 26 while (fgets(s[i], N, fp) != NULL) { 27 s[i][strcspn(s[i], "\n")] = '\0'; 28 ++i; 29 } 30 n = i; 31 32 srand((unsigned int)time(0)); 33 for (i = 0; i < M; ) { 34 lucky_i = rand() % n; 35 if (has_hit[lucky_i]) 36 continue; 37 has_hit[lucky_i] = 1; 38 strcpy(hit[i], s[lucky_i]); 39 ++i; 40 } 41 42 printf("-------------------中奖名单-------------------\n"); 43 for (i = 0; i < M; ++i) { 44 printf("%s\n", hit[i]); 45 } 46 printf("-------------保存到文件------------"); 47 48 printf("\n输入文件名:"); 49 fgets(tmp, 80, stdin); 50 tmp[strcspn(tmp, "\n")] = '\0'; 51 strcpy(filename, tmp); 52 53 fout = fopen(filename, "w"); 54 if (!fout) { 55 perror(filename); 56 fclose(fp); 57 return 1; 58 } 59 for (i = 0; i < M; ++i) { 60 fprintf(fout, "%s\n", hit[i]); 61 } 62 63 printf("保存成功!\n"); 64 65 fclose(fp); 66 fclose(fout); 67 68 return 0; 69 }
运行结果



浙公网安备 33010602011771号