申请该结构体数组,容量为5,初始化5个学生的信息 使用fprintf将数组中的5个学生信息,保存到文件中去 下一次程序运行的时候,使用fscanf,将文件中的5个学生信息,写入(加载)到数组中去,并直接输出学生信息
1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <pthread.h>
9 #include <semaphore.h>
10 #include <wait.h>
11 #include <signal.h>
12 #include <sys/socket.h>
13 #include <arpa/inet.h>
14 #include <sys/socket.h>
15 #include <sys/ipc.h>
16 #include <sys/sem.h>
17 #include <semaphore.h>
18 #include <sys/msg.h>
19 #include <sys/shm.h>
20 #include <sys/un.h>
21 typedef struct Student
22 {
23 char name[16];
24 int age;
25 double matg_score;
26 double chinese_score;
27 double english_score;
28 double physics_score;
29 double chemistry_score;
30 double bio_score;
31 }Student,*node_p;
32 int main(int argc, const char *argv[])
33 {
34 node_p H=(node_p)malloc(sizeof(Student)*5);
35 if(H==NULL)
36 {
37 printf("空间申请失败\n");
38 return -1;
39 }
40 strcpy((H)->name,"zhangshan");
41 (H+0)->age=20;(H+0)->matg_score=88.01;(H+0)->chinese_score=99.02;(H+0)->english_score=93.05;(H+0)->physics_score=94.04;(H+0)->chemistry_score=96.06;(H+0)->bio_score=99.99;
42 strcpy((H+1)->name,"lisi");
43 (H+1)->age=21;(H+1)->matg_score=88.11;(H+1)->chinese_score=99.12;(H+1)->english_score=93.15;(H+1)->physics_score=94.14;(H+1)->chemistry_score=96.16;(H+1)->bio_score=99.99;
44 strcpy((H+2)->name,"wangwu");
45 (H+2)->age=22;(H+2)->matg_score=88.21;(H+2)->chinese_score=99.22;(H+2)->english_score=93.25;(H+2)->physics_score=94.24;(H+2)->chemistry_score=96.26;(H+2)->bio_score=99.99;
46 strcpy((H+3)->name,"lingliu");
47 (H+3)->age=23;(H+3)->matg_score=88.31;(H+3)->chinese_score=99.32;(H+3)->english_score=93.35;(H+3)->physics_score=94.34;(H+3)->chemistry_score=96.36;(H+3)->bio_score=99.99;
48 strcpy((H+4)->name,"xiaoqi");
49 (H+4)->age=24;(H+4)->matg_score=88.41;(H+4)->chinese_score=99.42;(H+4)->english_score=93.45;(H+4)->physics_score=94.44;(H+4)->chemistry_score=96.46;(H+4)->bio_score=99.99;
50 FILE *fp=fopen("./06_tset.c","w");
51 if(fp==NULL)
52 {
53 perror("fopen");
54 return 1;
55 }
56 for(int i=0;i<5;i++)
57 {
58 fprintf(fp,"name=%s\t age=%d\t matg_score=%.2lf\t chinese_score=%.2lf\t english_score=%.2lf\t physics_score=%.2lf\t chemistry_score=%.2lf\t bio_score=%.2lf\n",(H+i)->name,(H+i)->age,(H+i)->matg_score,(H+i)->chinese_score,(H+i)->english_score,(H+i)->physics_score,(H+i)->chemistry_score,(H+i)->bio_score);
59 }
60 fclose(fp);
61 fp=fopen("./06_tset.c","r");
62 if(fp==NULL)
63 {
64 perror("报错信息");
65 return 1;
66 }
67 char arr[10000];
68 int i=0;
69 while(1)
70 {
71 int retval=fscanf(fp,"%c",&arr[i]);
72 if(retval==EOF)
73 {
74 break;
75 }
76 i++;
77 }
78 for(int j=0;j<i;j++)
79 {
80 printf("%c",arr[j]);
81 }
82 fclose(fp);
83 free(H);
84 H=NULL;
85 return 0;
86 }