save.c
#include <stdio.h>
#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 sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
/*************************************/
typedef struct students
{
char name[32];
int chinese;
int math;
int english;
int physical;
int chemical;
int biology;
}stu;//存储学生信息的结构体
/************************************/
//链表结构体
typedef struct Node
{
union
{
int len;
stu data;
};
struct Node*next;//指针域指向下一个节点
}linklist,*linklistptr;
/************************************/
linklistptr link_create();
int empty(linklistptr H);
linklistptr create_node(stu e);
int head_add(linklistptr H,stu e);
void output(linklistptr H);
void free_list(linklistptr H);
//主函数
int main(int argc, const char *argv[])
{
//1.创建链表
linklistptr H=link_create();
//2.插入学生的信息
stu student1={"张三",1,2,3,4,5,6};
stu student2={"李四",11,22,33,44,55,66};
stu student3={"王五",31,32,33,34,35,36};
head_add(H,student1);
head_add(H,student2);
head_add(H,student3);
//遍历
output(H);
/************/
//1.打开文件
FILE *fp=fopen("./main.c","w");
if(NULL==fp)
{
perror("fp open wrong");
return -1;
}
//2.将数据写入文件
linklistptr p=H->next;//跳过头结点
while (p != NULL) {
fprintf(fp, "%s %d %d %d %d %d %d\n", p->data.name, p->data.chinese, p->data.math,
p->data.english, p->data.physical, p->data.chemical, p->data.biology);
p = p->next;
}
fclose(fp);
free_list(H);
return 0;
}
//创建链表
linklistptr link_create()
{
linklistptr H=(linklistptr)malloc(sizeof(linklist));
if(NULL==H)
{
printf("创建链表失败\n");
return NULL;
}
//创建成功将头结点的数据域置0,指针域指向NULL
H->len=0;
H->next=NULL;
printf("学生链表创建成功\n");
return H;
}
//判断链表是否为空
int empty(linklistptr H)
{
if(NULL ==H)
{
printf("判断为空失败\n");
return -1;
}
return H->len==0;
}
//申请节点封装数据
linklistptr create_node(stu e)
{
//申请节点大小的空间
linklistptr p=(linklistptr)malloc(sizeof(linklist));
if(NULL == p)
{
printf("申请节点失败\n");
return NULL;
}
p->data=e;
p->next=NULL;
return p;
}
//头插
int head_add(linklistptr H,stu e)
{
//判断链表是否合法
if(NULL==H)
{
printf("头插失败\n");
return 0;
}
//申请节点封装数据
linklistptr p=create_node(e);
p->next=H->next;
H->next=p;
H->len++;
return 1;
}
//遍历
void output(linklistptr H)
{
if(NULL==H || empty(H))
{
printf("遍历失败\n");
return;
}
linklistptr q=H;
for(int i=0;i<H->len;i++)
{
q=q->next;
printf("姓名:%s\n",q->data.name);
printf("语文:%d\n",q->data.chinese);
printf("数学:%d\n",q->data.math);
printf("英语:%d\n",q->data.english);
printf("物理:%d\n",q->data.physical);
printf("化学:%d\n",q->data.chemical);
printf("生物:%d\n",q->data.biology);
printf("--------------------------\n");
}
}
// 释放链表内存
void free_list(linklistptr H) {
linklistptr p = H;
while (p != NULL) {
linklistptr temp = p;
p = p->next;
free(temp);
}
}
load.c
#include <stdio.h>
#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 sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
/***********************************/
typedef struct students
{
char name[32];
int chinese;
int math;
int english;
int physical;
int chemical;
int biology;
}stu;//存储学生信息的结构体
/********************************/
int main(int argc, const char *argv[])
{
//以只读的形式打开已写入的文件
FILE*fp=fopen("./main.c","r");
if(fp==NULL)
{
perror("fp open wrong\n");
return -1;
}
//创建读取的容器
stu student[3];
int i=0;
while(1)
{
fscanf(fp,"%s %d %d %d %d %d %d\n",student[i].name,
&student[i].chinese,&student[i].math,&student[i].english,
&student[i].physical,&student[i].chemical,&student[i].biology);
if(feof(fp)==1)
{
break;
}
i++;
}
fclose(fp);
for(int i=2;i>=0;i--){
printf("姓名:%s\n",student[i].name);
printf("语文:%d\n",student[i].chinese);
printf("数学:%d\n",student[i].math);
printf("英语:%d\n",student[i].english);
printf("物理:%d\n",student[i].physical);
printf("化学:%d\n",student[i].chemical);
printf("生物:%d\n",student[i].biology);
printf("---------------------\n\n");
}
return 0;
}