笔记
作业
有如下结构体
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个学生信息,写入(加载)到数组中去,并直接输出学生信息
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
#define MAXSIZE 5
typedef struct student{
char name[16];
int age;
double math_score;
double chinses_score;
double english_score;
double physics_score;
double chemistry_score;
double bio_score;
}stu,*stu_p;
stu_p create_stu()
{
stu_p S=(stu_p)malloc(sizeof(stu));
if(NULL==S)
{
printf("空间申请失败\n");
return NULL;
}
return S;
}
void Init_stu(stu_p *S,int age,double math_score,double chinses_score,double english_score,double physics_score,double chemistry_score,double bio_score)
{
if(NULL==*S)
{
printf("学生信息表不存在\n");
return;
}
//(*S)->name=name;
(*S)->age=age;
(*S)->math_score=math_score;
(*S)->chinses_score=chinses_score;
(*S)->english_score=english_score;
(*S)->physics_score=physics_score;
(*S)->chemistry_score=chemistry_score;
(*S)->bio_score=bio_score;
}
void print_stu(stu_p *S)
{
int i;
for(i=0;i<MAXSIZE;i++)
{
printf("%-16s ",S[i]->name);
printf("%-d ",S[i]->age);
printf("%-6.2lf ",S[i]->math_score);
printf("%-6.2lf ",S[i]->chinses_score);
printf("%-6.2lf ",S[i]->english_score);
printf("%-6.2lf ",S[i]->physics_score);
printf("%-6.2lf ",S[i]->chemistry_score);
printf("%-6.2lf ",S[i]->bio_score);
printf("\n");
}
}
int main(int argc, const char *argv[])
{
stu_p S[MAXSIZE];
int i;
for(i=0;i<MAXSIZE;i++)
{
S[i]=create_stu();
}
//这里直接把五名学生信息存入到数组1中
strcpy(S[0]->name,"zhangsan");
strcpy(S[1]->name,"lisi");
strcpy(S[2]->name,"wangwu");
strcpy(S[3]->name,"liuliu");
strcpy(S[4]->name,"luqi");
Init_stu(S,20,100,90,80,100,70,88);
Init_stu(S+1,21,99,92,89,98,79,80);
Init_stu(S+2,22,87,97,87,90,77,86);
Init_stu(S+3,18,89,80,80,99,75,88);
Init_stu(S+4,29,98,87,85,89,76,89);
//printf("数组S\n");
//print_stu(S);
FILE *fp;
fp=fopen(argv[1],"r");
if(NULL==fp)
{
//第一次执行,由于文件不存在,创建文件并把学生信息存入该文件
perror("第一次fopen");
fp=fopen(argv[1],"w");
for(i=0;i<MAXSIZE;i++)
{
fprintf(fp,"%-16s %-3d %-6.2lf %-6.2lf %-6.2lf %-6.2lf %-6.2lf %-6.2lf \n",S[i]->name,S[i]->age,S[i]->math_score,S[i]->chinses_score,S[i]->english_score,S[i]->physics_score,S[i]->chemistry_score,S[i]->bio_score);
}
fclose(fp);
}else {
//第二次执行,将学生信息存入第二个数组中
FILE *fp2=fopen(argv[1],"r");
if(NULL==fp2)
{
perror("第二次fopen");
return -1;
}
stu_p S2[MAXSIZE];
for(i=0;i<MAXSIZE;i++)
{
S2[i]=create_stu();
fscanf(fp2,"%s %d %lf %lf %lf %lf %lf %lf",S2[i]->name,&(S2[i]->age),&(S2[i]->math_score),&(S2[i]->chinses_score),&(S2[i]->english_score),&(S2[i]->physics_score),&(S2[i]->chemistry_score),&(S2[i]->bio_score));
}
fclose(fp2);
printf("数组S2\n");
print_stu(S2);
}
return 0;
}