这篇博客承接上一篇(https://blog.csdn.net/qq_41861442/article/details/99077592)加入了文件IO,每个简单程序都会面临这样的问题,数据无法保存,再次进入,数据全没了,那么我们就需要运用到文件IO的知识了,(没有数据库,bro)
在这里插入代码片#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
typedef struct node
{
int id_num;//teacher or student's number
char status;//teacher : t , student : s
int chinese;
int math;
int English;
int passwd;
char address[64];
char sex;//man : m , women : w
struct node *next;
}*listnode;
listnode listnode_init()
{
listnode head = malloc(sizeof(struct node)); //申请头结点空间
if(head == NULL)
{
perror("list init error");
return NULL;
}
head->next = NULL;
return head;//返回头结点地址
}
void data_init(listnode newnode ,int id_num,char status,int chinese,int math ,int English , int passwd,char * address,char sex)
{
newnode->id_num = id_num;
newnode->status = status;
newnode->chinese = chinese;
newnode->math = math;
newnode->English = English;
newnode->passwd = passwd;
strcpy(newnode->address,address);
newnode->sex = sex;
}
bool add_node(listnode head,int id_num,char status,int chinese,int math ,int English , int passwd,char * address,char sex)
{
listnode newnode = malloc(sizeof(struct node));
if(newnode == NULL)
{
perror("malloc newnode error");
return -1;
}
//2、初始化节点数据
data_init(newnode ,id_num,status,chinese,math ,English,passwd,address,sex);
//3找到链表尾部
listnode p = head;
while(p->next != NULL) //如果p->next不等于 NULL,证明后面还有节点
{
p = p->next;
}
newnode->next = p->next;
p->next = newnode;
printf("尾插法所接入的数据 %d,%c\n", p->next->id_num,p->next->status);
return true;
}
listnode search_member_prev(listnode head ,int id_number)
{
listnode tmp = head ;
while(tmp->next != NULL)
{
if(tmp->next->id_num == id_number)
{
return tmp ;
}
tmp = tmp->next;
}
return NULL;
}
//传入头节点,和另一个节点
//函数中输入 id_num 和 passwd,若匹配,返回true
listnode login_in(listnode head,listnode newOne)
{
int passwdInput,id_num;
listnode new = head;
printf("please input your id_num(five Number): ");
if(scanf("%d",&(id_num)) == 1)
{
while(getchar() != '\n');
listnode tmp = search_member_prev(head,id_num);
if(tmp != NULL)
{
printf("please input your password :");
scanf("%d",&passwdInput);
while(getchar() !=