有精通c++的大神吗?该怎么修改程序让我可以把输入的信息保存到一个文件里在下次打开的时候能读取出来

liwotorie 2017-06-07 11:03:07
#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;
}
...全文
2444 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
墨梅无痕 2021-03-06
  • 打赏
  • 举报
回复
都是大佬! 人家明明在学习结构、自定义结构的文件操作,,你上来就给人家整个 INI 文件操作,瞎耽误功夫。
ooolinux 2021-03-06
  • 打赏
  • 举报
回复
信息可以保存在文本文件里,程序运行的时候用文件重定向输入信息,不用手动输入。
茹果伱在 2021-02-08
  • 打赏
  • 举报
回复
用流
weixin_45241387 2019-06-14
  • 打赏
  • 举报
回复
有文件吗发给我下大哥
weixin_45241387 2019-06-14
  • 打赏
  • 举报
回复
引用 2 楼 zhujinqiang的回复:
c++builder,读写ini文件

//包含文件
#include <inifiles.hpp>

//写文件

TIniFile *ini;
ini=new TIniFile(ChangeFileExt(ExtractFilePath(Application->ExeName),"config.cfg"));
ini->WriteInteger("Form","Top",Top);
ini->WriteInteger("Form","Left",Left);
ini->WriteString("Form","Caption",Caption);
ini->WriteBool("Form","InitMax",WindowState==wsMaximized);
delete ini;

//读文件

if(FileExists(ExtractFilePath(Application->ExeName)+"config.cfg"))//检测文件是否存在
{
TIniFile *ini;
ini=new TIniFile(ChangeFileExt(ExtractFilePath(Application->ExeName),"config.cfg"));

Top=ini->ReadInteger("Form","Top",100);
Left=ini->ReadInteger("Form","Left",100);
Caption =ini->ReadString("Form","Caption","Default Caption");
ini->ReadBool("Form","InitMax",false)?WindowState=wsMaximized:WindowState=wsNormal;
delete ini;
}
else
{
ShowMessage("文件存在");
}
大神我也要这个交作业但是不会搞可以帮下吗
weixin_45241387 2019-06-14
  • 打赏
  • 举报
回复
引用 5 楼 华山沦贱的回复:
经典的用ini,好的串口工具都能讲最后一次的设置保存下来供下次初始化
大哥我也需要这个但是我复制过去的不可以用
华山沦贱 2017-07-16
  • 打赏
  • 举报
回复
经典的用ini,好的串口工具都能讲最后一次的设置保存下来供下次初始化
qq_30422087 2017-06-15
  • 打赏
  • 举报
回复
同意楼上的,创建一个ini文件,读写ini文件就可以了
liwotorie 2017-06-07
  • 打赏
  • 举报
回复
具体要怎么修改啊?那个大神帮帮我
zhujinqiang 2017-06-07
  • 打赏
  • 举报
回复
参考: http://blog.csdn.net/m_buddy/article/details/54097131 http://blog.csdn.net/gsnet/article/details/8614179
zhujinqiang 2017-06-07
  • 打赏
  • 举报
回复
c++builder,读写ini文件

//包含文件
#include <inifiles.hpp>

//写文件

TIniFile *ini;
ini=new TIniFile(ChangeFileExt(ExtractFilePath(Application->ExeName),"config.cfg"));
ini->WriteInteger("Form","Top",Top);
ini->WriteInteger("Form","Left",Left);
ini->WriteString("Form","Caption",Caption);
ini->WriteBool("Form","InitMax",WindowState==wsMaximized);
delete ini;

//读文件

if(FileExists(ExtractFilePath(Application->ExeName)+"config.cfg"))//检测文件是否存在
{
TIniFile *ini;
ini=new TIniFile(ChangeFileExt(ExtractFilePath(Application->ExeName),"config.cfg"));

Top=ini->ReadInteger("Form","Top",100);
Left=ini->ReadInteger("Form","Left",100);
Caption =ini->ReadString("Form","Caption","Default Caption");
ini->ReadBool("Form","InitMax",false)?WindowState=wsMaximized:WindowState=wsNormal;
delete ini;
}
else
{
ShowMessage("文件存在");
}

552

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 茶馆
社区管理员
  • 茶馆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧