思维导图:
练习:
有如下结构体 struct Student{ char name[16]; int age; double math_score; double chinese_score; double english_score; double physics_score; double chemistry_score; double bio_score; }; 申请该结构体数组,容量为5,初始化5个学生的信息 使用fprintf将数组中的5个学生信息,保存到文件中去 下一次程序运行的时候,使用fscanf,将文件中的5个学生信息,写入(加载)到数组中去,并直接输出学生信息
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <sys/types.h> 5 #include <unistd.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 math_score; 26 double chinese_score; 27 double english_score; 28 double physics_score; 29 double chemistry_score; 30 double bio_score; 31 }information; 32 int main(int argc, const char *argv[]) 33 { 34 //向堆区空间申请容量为5的内存空间 35 information *students=(information*)malloc(5*sizeof(information)); 36 if(!students) 37 { 38 perror("students:"); 39 return -1; 40 } 41 //循环写入学生信息 42 for(int i=0;i<5;i++) 43 { 44 printf("输入学生姓名:"); 45 scanf(" %15s",students[i].name); 46 printf("输入学生年龄:"); 47 scanf(" %d",&students[i].age); 48 printf("输入学生数学成绩:"); 49 scanf(" %lf",&students[i].math_score); 50 printf("输入学生语文成绩:"); 51 scanf(" %lf",&students[i].chinese_score); 52 printf("输入学生英语成绩:"); 53 scanf(" %lf",&students[i].english_score); 54 printf("输入学生化学成绩:"); 55 scanf(" %lf",&students[i].chemistry_score); 56 printf("输入学生生物成绩:"); 57 scanf(" %lf",&students[i].bio_score); 58 } 59 //打开文件 60 FILE *wfp=fopen(argv[1],"w"); 61 if(!wfp) 62 { 63 perror("wfp:"); 64 return -1; 65 } 66 //将信息录入文件中 67 for(int i=0;i<5;i++) 68 { 69 fprintf(wfp,"姓名:%s\n年龄:%d\n数学成绩:%.1f\n语文成绩:%.1f\n英语成绩:%.1f\n化学成绩:%.1f\n生物成绩:%.1f\n",\ 70 students[i].name,students[i].age,students[i].math_score,students[i].chinese_score,students[i].english_score,\ 71 students[i].chemistry_score,students[i].bio_score); 72 } 73 //关闭文件 74 fclose(wfp); 75 //打开文件 76 FILE *rfp=fopen(argv[2],"r"); 77 if(!rfp) 78 { 79 perror("rfp:"); 80 return -1; 81 } 82 //循环将文件内容赋值给结构体数组 83 for(int i=0;i<5;i++) 84 { 85 if(fscanf(rfp,"姓名:%15s\n年龄:%d\n数学成绩:%lf\n语文成绩:%lf\n英语成绩:%lf\n化学成绩:%lf\n生物成绩:%lf\n",students[i].name,&students[i].age,&students[i].math_score,\ 86 &students[i].chinese_score,&students[i].english_score,&students[i].chemistry_score,&students[i].bio_score)!=7) 87 { 88 perror("rfp:"); 89 free(students); 90 return -1; 91 } 92 } 93 //循环输出数组元素 94 for(int i=0;i<5;i++) 95 { 96 printf("姓名:%s\n年龄:%d\n数学成绩:%.1f\n语文成绩:%.1f\n英语成绩:%.1f\n化学成绩:%.1f\n生物成绩:%.1f\n",\ 97 students[i].name,students[i].age,students[i].math_score,students[i].chinese_score,students[i].english_score,\ 98 students[i].chemistry_score,students[i].bio_score); 99 } 100 //关闭文件 101 fclose(rfp); 102 //释放内存 103 free(students); 104 students=NULL; 105 return 0; 106 } ~ ~ ~ ~ ~