申请该结构体数组,容量为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>
0 #include <wait.h>
1 #include <signal.h>
2 #include <sys/socket.h>
3 #include <arpa/inet.h>
4 #include <sys/socket.h>
5 #include <sys/ipc.h>
6 #include <sys/sem.h>
7 #include <semaphore.h>
8 #include <sys/msg.h>
9 #include <sys/shm.h>
0 #include <sys/un.h>
1 typedef struct Student
2 {
3 char name[16];
4 int age;
5 double matg_score;
6 double chinese_score;
7 double english_score;
8 double physics_score;
9 double chemistry_score;
0 double bio_score;
1 }Student,*node_p;
2 int main(int argc, const char *argv[])
3 {
4 node_p H=(node_p)malloc(sizeof(Student)*5);
5 /*if(H=NULL)
6 {
7 printf("空间申请失败\n");
8 return -1;
9 }*/
0 strcpy((H)->name,"zhangshan");
1 (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;
2 strcpy((H+1)->name,"lisi");
3 (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;
4 strcpy((H+2)->name,"wangwu");
5 (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;
6 strcpy((H+3)->name,"lingliu");
7 (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;
8 strcpy((H+4)->name,"xiaoqi");
9 (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;
0 FILE *fp=fopen("./06_tset.c","w");
1 if(fp==NULL)
2 {
3 perror("fopen");
4 return 1;
5 }
6 for(int i=0;i<5;i++)
7 {
8 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);
9 }
0 fclose(fp);
1 fp=fopen("./06_tset.c","r");
2 char arr[10000];
3 int i=0;
4 while(1)
5 {
6 int retval=fscanf(fp,"%c",&arr[i]);
7 if(retval==EOF)
8 {
9 break;
0 }
1 i++;
2 }
3 for(int j=0;j<i;j++)
4 {
5 printf("%c",arr[j]);
6 }
7 fclose(fp);
8 free(H);
9 H=NULL;
0 return 0;
1 }