daizybroa

导航

work7

work1

 

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

 

 work2

 1 #include <stdio.h>
 2 
 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     return 0;
22 }
23 
24 void write() {
25     Book x[] = { {"《雕塑家》", "斯科特.麦克劳德"},
26                   {"《灯塔》", "克里斯多夫.夏布特"},
27                   {"《人的局限性》", "塞缪尔.约翰生"},
28                   {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"},
29                   {"《大地之上》", "罗欣顿·米斯特里"},
30                   {"《上学记》", "何兆武"},
31                   {"《命运》", "蔡崇达"} };
32     int n;
33     FILE* fp;
34 
35     // 计算数组x中元素个数
36     n = sizeof(x) / sizeof(x[0]);
37 
38     // 以写的方式打开二进制文件data2.dat
39     fp = fopen("data2.dat", "wb");
40 
41     // 如果打开文件失败,输出提示信息并返回 
42     if (fp == NULL) {
43         printf("fail to open file to write\n");
44         return;
45     }
46 
47     // 将结构体数组x中的图书信息以数据块方式写入二进制文件data2.dat
48     fwrite(x, sizeof(Book), n, fp);
49 
50     fclose(fp);
51 }
52 
53 void read() {
54     Book x[M];
55     int i, n;
56     int number;
57 
58     FILE* fp;
59 
60     // 以读的方式打开二进制文件data2.dat
61     fp = fopen("data2.dat", "rb");
62 
63     // 如果打开文件失败,输出提示信息并返回 
64     if (fp == NULL) {
65         printf("fail to open file to read\n");
66         return;
67     }
68 
69     // 从二进制文件data2.dat以数据块方式读取图书信息存储到结构体数组x
70     i = 0;
71     while (!feof(fp)) {
72         number = fread(&x[i], sizeof(Book), 1, fp);
73         if (number != 1)
74             break;
75         i++;
76     }
77 
78     // 在屏幕上打印输出
79     n = i;
80     for (i = 0; i < n; ++i)
81         printf("%d. %-40s%-20s\n", i + 1, x[i].name, x[i].author);
82 
83     fclose(fp);
84 }

 

 

 work 3

 1 // 文件读写操作:以字符、字符串形式读、写
 2 
 3 #include <stdio.h>
 4 #define N 5
 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[N] = { "Working\'s Blues",
26                      "Everything Will Flow",
27                      "Streets of London",
28                      "Perfect Day",
29                      "Philadelphia"};
30     int i;
31     FILE *fp;
32 
33     fp = fopen("data3.txt", "w");
34     if(!fp) {
35         printf("fail to open file to write\n");
36         return;
37     }
38 
39     for(i = 0; i < N; ++i) {
40         fputs(ptr[i], fp);
41         fputs("\n", fp);
42     }
43     
44     fclose(fp);
45 }
46 
47 void read_str() {
48     char songs[N][M];
49     int i;
50     FILE *fp;
51 
52     fp = fopen("data3.txt", "r");
53     if(!fp) {
54         printf("fail to open file to read\n");
55         return;
56     }
57 
58     for(i = 0; i < N; ++i)
59         fgets(songs[i], 80, fp);
60 
61     for(i = 0; i < N; ++i)
62         printf("%d. %s", i+1, songs[i]);
63     
64     fclose(fp);
65 }
66 
67 void read_char() {
68     char ch;
69     FILE *fp;
70 
71     fp = fopen("data3.txt", "r");
72     if(!fp) {
73         printf("fail to open file to read\n");
74         return;
75     }
76 
77     while(!feof(fp)) {
78         ch = fgetc(fp);
79         if(ch == EOF)
80             break;
81         
82         putchar(ch);
83     }
84 
85     fclose(fp);
86 }

work 4

 1 #define _CRT_SECURE_NO_WARNINGS 1 
 2 #include<stdio.h>
 3 int main() {
 4     FILE *fp;
 5     int line = 1, words = 0;
 6     char n;
 7 
 8     fp = fopen("data4.txt", "r");
 9     if (!fp){
10         printf("fall to open the file for reading\n");
11         return 0;
12     }
13 
14     while (!feof(fp)) {
15         n = fgetc(fp);
16         if (n=='\n')
17             line++;
18         if ((n >= 'a' && n <= 'z') || (n >= 'A' && n <= 'Z') || (n >= '0' && n <= '9') || n == '-')
19             words++;
20     }
21 
22     printf("data4.txt统计结果:\n");
23     printf("行数:%d\n", line);
24     printf("字符数为:%d\n",words);
25 
26     fclose(fp);
27 
28     return 0;
29 }

 work 5

  1 #include <string.h>
  2 #define N 10
  3 
  4 typedef struct {
  5     long id;            // 准考证号
  6     char name[20];      // 姓名
  7     float objective;    // 客观题得分
  8     float subjective;   // 操作题得分
  9     float sum;          // 总分
 10     char result[10];    // 考试结果
 11 } STU;
 12 
 13 // 函数声明
 14 void read(STU st[], int n);
 15 void write(STU st[], int n);
 16 void output(STU st[], int n);
 17 int process(STU st[], int n, STU st_pass[]);
 18 
 19 int main() {
 20     STU stu[N], stu_pass[N];
 21     int cnt;
 22     double pass_rate;
 23 
 24     printf("从文件读入%d个考生信息...\n", N);
 25     read(stu, N);
 26 
 27     printf("\n对考生成绩进行统计...\n");
 28     cnt = process(stu, N, stu_pass);
 29 
 30     printf("\n通过考试的名单:\n");
 31     output(stu, N);   // 输出所有考生完整信息到屏幕
 32     write(stu, N);    // 输出考试通过的考生信息到文件
 33 
 34     pass_rate = 1.0 * cnt / N;
 35     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100);
 36 
 37     return 0;
 38 }
 39 
 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, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
 48 }
 49 
 50 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 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     while (!feof(fin)) {
 62         for (i = 0; i < n; i++)
 63             fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 64     }
 65 
 66     fclose(fin);
 67 }
 68 
 69 // 把通过考试的考生完整信息写入文件list_pass.txt
 70 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 71 void write(STU s[], int n) {
 72     FILE* fp;
 73     int i;
 74     fp = fopen("list_pass.txt", "w");
 75     if (!fp)
 76     {
 77         printf("fail to open the file\n");
 78         return;
 79     }
 80     fprintf(fp, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n");
 81     for (i = 0; i < n; i++)
 82     {
 83         if (s[i].sum >= 60){
 84             fprintf(fp, "%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result);
 85         }
 86     }
 87     fclose(fp);
 88 
 89 }
 90     
 91 
 92 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 93 int process(STU st[], int n, STU st_pass[]) {
 94     int num = 0;
 95     int i;
 96     int j = 0;
 97     for (i = 0; i < n; i++) {
 98         st[i].sum = st[i].subjective + st[i].objective;
 99         if (st[i].sum >= 60) {
100             strcpy(st[i].result, "通过");
101             st_pass[j] = st[i];
102             j++;
103             num++;
104         }
105         else
106             strcpy(st[i].result, "不通过");
107     }
108     return num;
109 
110 }

 work6

 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<time.h>
 5 #define N 80
 6 int main() {
 7     char info[N][80];
 8     int flag[N] = { 0 };
 9     int i;
10     int lucky_index;
11     int win[N];
12     char filename[N];
13     FILE* fp;
14 
15     fp = fopen("list.txt", "r");
16     if (!fp) {
17         printf("fail to open file to read\n");
18         return 1;
19     }
20 
21     for (i = 0; i < N; i++)
22         fgets(info[i], 80, fp);
23     fclose(fp);
24 
25     srand(time(NULL));
26     printf("------中奖名单------\n");
27     for (i = 0; i < 5; i++) {
28         lucky_index = rand() % N;
29         while (flag[lucky_index])
30             lucky_index = rand() % N;
31         flag[lucky_index] = 1;
32         printf("%s", info[lucky_index]);
33         win[i] = lucky_index;
34     }
35     printf("------保存到文件------\n");
36     printf("输入文件名:");
37     scanf("%s", filename);
38     fp = fopen(filename, "w");
39     if (!fp) {
40         printf("fail to open file to write\n");
41         return 1;
42     }
43     for (i = 0; i < 5; i++) {
44         fputs(info[win[i]], fp);
45         fputs("\n", fp);
46     }
47     fclose(fp);
48     printf("保存成功\n");
49     system("pause");
50     return 0;
51 }

 

 

posted on 2025-06-09 13:21  孤独在一方的星  阅读(10)  评论(0)    收藏  举报