有精通c++的大神吗?该怎么修改程序让我可以把输入的信息保存到一个文件里在下次打开的时候能读取出来
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct student{
char name[20];
char address[40];
char phonenum[30];
char postcode[20];
student *next;
};
//定义数据结构体
void mainscreen(student *p);
void title();
void save(student *head)
{
FILE *fp;
student *point;
point=head;
if((fp=fopen("data.dat","wb"))==NULL)
{
cout<<"can not open file"<<endl;
exit(0);
}
while (point)
{
fwrite(point,1, sizeof(struct student),fp);
point=point->next;
}
fclose(fp);
}
void read_(student *head) //文件数据读取函数
{
FILE *p=fopen("data.dat","rb");
fread(head,sizeof(struct student),1,p);
/*while(head)
{
cout<<"%s\n",head->name);
cout<<"%s\n",head->address);
cout<<"%s\n",head->phonenum);
head=head->next;
}
*/
}
void assert_student(student *head)
{
system("cls");
title();
static student *p,*q;
if(head==NULL)
{
head=q=(struct student *)malloc(sizeof(student));
p=(struct student *)malloc(sizeof(student));
head->next=p;
cout<<"当前通讯录未初始化,请输入首对象的姓名:";
cin>>p->name;
cout<<"请输入对象的家庭住址:";
cin>>p->address;
cout<<"请输入对象的电话号码:";
cin>>p->phonenum;
cout<<"请输入对象的邮政编码:";
cin>>p->postcode;
q->next=p;
q=p;
q->next=NULL;
system("cls");
title();
}
else if(head!=NULL)
{
p=(struct student *)malloc(sizeof(student));
cout<<"请输入对象的姓名:";
cin>>p->name;
cout<<"请输入对象的家庭住址:";
cin>>p->address;
cout<<"请输入对象的电话号码:";
cin>>p->phonenum;
cout<<"请输入对象的邮政编码:";
cin>>p->postcode;
q->next=p;
q=p;
q->next=NULL;
system("cls");
title();
}
mainscreen(head);
}
void delete_student(student *head)
{
int flag=1;
system("cls");
char de_stu[20];
cout<<"请输入要注销信息的姓名:";
cin>>de_stu;
student *q,*front;
front=(struct student *)malloc(sizeof(student));
q=head->next;
while(q!=NULL)
{
if(strcmp(q->name,de_stu)==0)
{
cout<<"已找到需要注销的信息,正在注销中…………"<<endl;
q=front->next;
front->next=q->next;
cout<<"该数据已注销成功,返回主页面…………"<<endl;
flag=0;
break;
}
q=q->next;
}
if(flag==1)
cout<<"查找的信息不存在"<<endl;
mainscreen(head);
}
void change_student(student *head)
{
system("cls");
title();
int flag=1;
student *q;
char name_stu[20];
cout<<"请输入你要修改信息的姓名:";
cin>>name_stu;
q=head->next;
while(q!=NULL)
{
if(strcmp(q->name,name_stu)==0)
{
cout<<"请开始个人信息的修改:"<<endl;
cout<<"姓名:";
cin>>q->name;
cout<<"家庭住址:";
cin>>q->address;
cout<<"电话号码:";
cin>>q->phonenum;
cout<<"邮政编码:";
cin>>q->postcode;
cout<<"个人信息修改完成!"<<endl;
flag=0;
break;
}
q=q->next;
}
if(flag==1)
cout<<"查找的信息不存在"<<endl;
mainscreen(head);
}
void search_student(student *head)
{
system("cls");
title();
int flag=1;
char name_stu[20];
cout<<"请输入你要查找的姓名:";
cin>>name_stu;
student *q;
q=head->next;
while(q!=NULL)
{
if(strcmp(q->name,name_stu)==0)
{
cout<<"姓名:"<<" ";
cout<<"家庭住址:"<<" ";
cout<<"电话号码:"<<" "<<endl;
cout<<"邮政编码:"<<" "<<endl;
cout<<q->name<<" ";
cout<<q->address<<" ";
cout<<q->phonenum<<" "<<endl;
cout<<q->postcode<<" "<<endl;
flag=0;
break;
}
q=q->next;
}
if(flag==1)
cout<<"查找的信息不存在"<<endl;
mainscreen(head);
}
void show_student(student *head)
{
system("cls");
title();
student *q;
q=head->next;
while(q!=NULL)
{
cout<<"姓名:"<<" ";
cout<<"家庭住址:"<<" ";
cout<<"电话号码:"<<" "<<endl;
cout<<"邮政编码:"<<" "<<endl;
cout<<q->name<<" ";
cout<<q->address<<" ";
cout<<q->phonenum<<" "<<endl;
cout<<q->postcode<<" "<<endl;
q=q->next;
}
mainscreen(head);
}
void title()
{
char a=3;
for(int i=0;i<90;i++)
cout<<a;
cout<<endl;
cout<<" ********通讯管理系统******** "<<endl;
a=5;
for(i=0;i<90;i++)
cout<<a;
cout<<endl;
}
void mainscreen(student *head)
{
int choice;
char a=14;
cout<<a<<"请按照要求选择要进行的操作:"<<endl;
cout<<a<<"按照信息后的数字提示选择相应的操作:"<<endl<<endl;
for(int i=0;i<68;i++)
cout<<a;
cout<<endl;
cout<<" "<<a<<" 插入新的人物通讯录**************************************** 1 "<<a<<endl;
cout<<" "<<a<<" 根据姓名查找相应的人物信息******************************** 2 "<<a<<endl;
cout<<" "<<a<<" 根据姓名删除相应的人物信息******************************** 3 "<<a<<endl;
cout<<" "<<a<<" 根据姓名修改相应的人物信息******************************** 4 "<<a<<endl;
cout<<" "<<a<<" 退出通讯录登录系统****************************************** 5 "<<a<<endl;
cout<<" "<<a<<" 显示当前数据库中所有的通讯录信息************************* 6 "<<a<<endl;
cout<<" "<<a<<" 读取当前数据库中所有的通讯录信息************************* 7 "<<a<<endl;
cout<<" "<<a<<" 保存当前数据库中所有的通讯录信息************************* 8 "<<a<<endl;
for(i=0;i<68;i++)
cout<<a;
cout<<endl;
cout<<"请输入你的选择:";
cin>>choice;
switch(choice)
{
case 1:assert_student(head);break;
case 2:search_student(head);break;
case 3:delete_student(head);break;
case 4:change_student(head);break;
case 5:save(head);exit(0);
case 6:show_student(head);break;
case 7:read_student(head);break;
case 8:save_student(head);break;
}
}
int main()
{
char a;
student *head;
head=NULL;
title();
a=2;
cout<<a<<"欢迎进入通讯录管理系统"<<endl;
int selectNum;
cout<<"\n请选择你接下来的操作"<<endl; //输出一个选择菜单
cout<<"1 进入通讯录管理系统"<<endl;
cout<<"2 退出通讯录管理系统"<<endl;
cin>>selectNum; //接收输入的选择数字
switch (selectNum) //按照输入的选择数调用函数
{
case 1:mainscreen(head);//进入菜单界面
break;
case 2:system("cls");exit(0); //调用退出函数
break;
}
return 0;
}