思维导图
1.学生信息写入
存在如下结构体
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>
typedef struct Student
{
char name[16];
int age;
double math;
double chinese;
double english;
double physic;
double chemistry;
double biological;
}student;
int main(int argc, const char *argv[])
{
student stu[5]={{"ikun",18,100,100,100,100,100,100},
{"cxk",19,90,90,90,90,90,90},
{"mjq",20,80,80,80,80,80,80},
{"wzy",21,70,70,70,70,70,70},
{"fcc",22,66,66,66,66,66,66}
};
FILE *fp=fopen("./1.txt","w");
if(fp==NULL)
{
perror("fopen");
return 1;
}
for(int i=0;i<5;i++)
{
fprintf(fp,"student%d:\n",i+1);
fprintf(fp,"name:%s\n",stu[i].name);
fprintf(fp,"age:%d\n",stu[i].age);
fprintf(fp,"math score:%.2lf\n",stu[i].math);
fprintf(fp,"chinese score:%.2lf\n",stu[i].chinese);
fprintf(fp,"english score:%.2lf\n",stu[i].english);
fprintf(fp,"physic score:%.2lf\n",stu[i].physic);
fprintf(fp,"chemistry score:%.2lf\n",stu[i].chemistry);
fprintf(fp,"biological score:%.2lf\n",stu[i].biological);
fprintf(fp,"\n");
}
fclose(fp);
fp=fopen("./1.txt","r");
if(fp==NULL)
{
perror("fopen");
return 1;
}
char str[99999];
int i=0;
while(1)
{
char retval=fscanf(fp,"%c",str+i);
if(retval==EOF)
{
break;
}
i++;
}
fclose(fp);
int len=strlen(str);
for(int j=0;j<len;j++)
{
printf("%s\n",str+j);
}
return 0;
}