通讯录存储结构是链表形式,类创建的通讯录怎么保存信息到txt文件并读取啊??是C++课设

我的Save和Read函数好像不行
下面是我的完整代码
求求哪位大佬帮我加这两个函数,课设要答辩了,来不及辽!!!!

#include <iostream>
#include <string>
#include <fstream>
#include <string>
using namespace std;

typedef int contacttype;

class contact{
public:
     string name;
     string address;
     string phonenum;
     contact *next;
     contacttype ctype;
     contact(){
        name="0";
        address="0";
        phonenum="0";
        ctype=0;
        next=NULL;
     }
};

//定义联系人结点类

class contactlist{
public:
    contact *head;
    contactlist(){
        head=new contact();
    }
    void Mainscreen(contact *dead);
    void UserReg(contact *head);
    void Login(contact *head);
    void Title();
    void Assert(contact *head);
    void Search(contact *head);
    void Delete(contact *head);
    void Change(contact *head);
    void Show(contact *head);
                void TypeShow(contact *head);
               friend void typeshow(contact *head,int choice);
    void Save(contact *head);
    void Read(contact *head);
};//定义联系人链表类


void contactlist::UserReg(contact *head)
{
    system("cls");                             
    FILE *fp;                                  
    char Linedata[50]={0},User[20],Pass[20];   
    fp = fopen("data.dat","at");             
    cout<<"******欢迎进入新账号注册新账号注册:******"<<endl;         
    cout<<"请输入用户名:";       
    fflush(stdin);                           
    gets(User);                                 
    cout<<"请输入密码:";
    fflush(stdin);                             
    gets(Pass);                                 
    strcpy(Linedata,User);                     
    strcat(Linedata,",");
    strcat(Linedata,Pass);
    strcat(Linedata,"\n");
    fputs(Linedata,fp);                        
    fclose(fp);                                
    cout<<"注册成功!"<<endl;
    system("pause");
    system("cls");
    Title();
    cout<<"等待转入登陆界面………………"<<endl;
    Login(head);
 }



void contactlist::Login(contact *head)//登陆
{
    FILE *fp;                                   
    int find=0;
    char User[20],Pass[20],Userstrcat[50]={0}; 
    char c,Userdata[50]={0};                     
    fp = fopen("data.dat","r");              
    cout<<"用户登录:"<<endl;
    cout<<"请输入用户名:";        
    fflush(stdin);                                
    gets(User);                               
    cout<<"请输入密码:";
    fflush(stdin);                                
    gets(Pass);                                    
    strcpy(Userstrcat,User);                      
    strcat(Userstrcat,",");
    strcat(Userstrcat,Pass);
    strcat(Userstrcat,"\n");
     while (!feof(fp))                             
     {
          fgets(Userdata,19,fp);
          if (strcmp(Userdata,Userstrcat)==0 )   
          {
              cout<<"登录成功!"<<endl;     
              find=1;      
                            Read(head);   //登陆成功后读取文件信息,保存在head链表里
                    system("cls");
                   Title();
              Mainscreen(head);                          
     }
    fclose(fp);                                        
    if (!find)                                          
    { 
       cout<<"用户名不存在或密码错误"<<endl; 
       cout<<"请选择重新登陆(按数字1)或者注册新账号(按数字2):";
       cin>>c;
       if(c=='1')  Login(head);
       else if(c=='2') contactlist::UserReg(head);
    }
}



void contactlist::Save(contact *head)
{
    FILE *fp;
    contact *point;
    point=head;
    fp=fopen("G:\\temp - 副本2\\lin.txt","wb+");
    if(fp==NULL)
    {
        cout<<"can not open file"<<endl;
        exit(0);
    }
    else{
       while (point)
       {

          fwrite(point, sizeof(class contact),1, fp);
          point=point->next;
       } 
       }
    fclose(fp);
}

/*
void contactlist::Read(contact *head)         
{
    FILE *p=fopen("G:\\temp - 副本2\\lin.txt","rb");
    fread(head,sizeof(class contact),1,p);
    while(head->next){
        head=head->next;
        fread(head,sizeof(class contact),1,p);
}
*/

void contactlist::Assert(contact *head)
{ 
  system("cls");
  Title();
  contact *p,*q;
  /*
  if(head->next==NULL)
  { 
     p=new contact();

     cout<<"当前通讯录为空,请输入第一个记录的联系人姓名:";
     cin>>p->name;
     cout<<"请输入联系人的家庭住址:";
     cin>>p->address;
     cout<<"请输入联系人的手机号码:";
     cin>>p->phonenum;
     cout<<"1:朋友"<<endl;
     cout<<"2:同学"<<endl;
     cout<<"3:亲戚"<<endl;
     cout<<"4:同事"<<endl;
     cout<<"请选择联系人的分组:";
     cin>>p->ctype;
     head->next=p;
     p->next=NULL;
     system("cls");
     Title();
  }
   else if(head->next!=NULL)
   { */
      q=new contact;
      q=head;
      p=new contact;
      cout<<"请输入联系人的姓名:";
      cin>>p->name;
      cout<<"请输入联系人的家庭住址:";
      cin>>p->address;
      cout<<"请输入联系人的手机号码:";
      cin>>p->phonenum;
      cout<<"联系人分类:"<<endl;
      cout<<"1:朋友"<<endl;
      cout<<"2:同学"<<endl;
      cout<<"3:亲戚"<<endl;
      cout<<"4:同事"<<endl;
      cout<<"请选择联系人的分组:";
      cin>>p->ctype;
      p->next=q->next;
      q->next=p;
      system("cls");
      Title();
//}
   contactlist::Mainscreen(head);
}



void contactlist::Delete(contact *head)
{ 
    int flag=1;
    system("cls");
    char de_con[10];
    cout<<"请输入要删除记录的联系人姓名:";
    cin>>de_con;
    contact *q,*front;//
    front=new contact;
    q=head;
    while((q->next)!=NULL)
    {   
        char Name[20];
        strcpy(Name, (q->next->name).c_str());
        if(strcmp(de_con,Name)==0)
        { 
             cout<<"已找到需要注销的记录,正在注销中…………"<<endl;
             q->next=q->next->next;
             cout<<"该数据已注销成功,返回主页面…………"<<endl;
             flag=0;
             break;
        }
        q=q->next;
    }
    if(flag==1)
    cout<<"要删除的记录不存在"<<endl;
    contactlist::Mainscreen(head);
}


void contactlist::Change(contact *head)
{
    system("cls");
    Title();
    int flag=1;
    contact *q;
    char name_con[10];
    cout<<"请输入你要修改记录的联系人姓名:";
    cin>>name_con;
    q=head->next;
    while(q!=NULL)
    { char Name[20];
        strcpy(Name, (q->name).c_str());
        if(strcmp(Name,name_con)==0)
        { 

            cout<<"请开始记录的修改:"<<endl;
            cout<<"姓名:";
            cin>>q->name;
            cout<<"家庭住址:";
            cin>>q->address;
            cout<<"电话号码:";
            cin>>q->phonenum;
            cout<<"联系人分组:";
            cout<<"1:朋友"<<endl;
            cout<<"2:同学"<<endl;
            cout<<"3:亲戚"<<endl;
            cout<<"4:同事"<<endl;
            cout<<"请选择联系人的类型:";
            cin>>q->ctype;
            cout<<"个人信息修改完成!"<<endl;
            flag=0;
            break;
        }
        q=q->next;
    }
    if(flag==1)
    cout<<"查找的记录不存在"<<endl;
    contactlist::Mainscreen(head);
}


void contactlist::Search(contact *head)
{
    system("cls");
    Title();
    int flag=1;
    char name_con[10];
    cout<<"请输入你要查找记录的联系人姓名:";
    cin>>name_con;
    contact *q;
    q=head->next;
    while(q!=NULL)
    {char Name[20];
        strcpy(Name, (q->name).c_str());
        if(strcmp(Name,name_con)==0)
        { 
            cout<<"姓名:"<<"       ";
            cout<<"家庭住址:"<<"       ";
            cout<<"电话号码:"<<"       ";
            cout<<"联系人分组:"<<"         "<<endl; 
            cout<<q->name<<"         ";
            cout<<q->address<<"            ";
            cout<<q->phonenum<<"           ";
            switch(q->ctype){
            case 1:cout<<"朋友           "<<endl;break;
            case 2:cout<<"同学           "<<endl;break;
            case 3:cout<<"亲戚           "<<endl;break;
            case 4:cout<<"同事           "<<endl;break;
            }
            flag=0;
            break;
        }
         q=q->next;
    }
     if(flag==1)
     cout<<"查找的记录不存在"<<endl;
     contactlist::Mainscreen(head);
}



void contactlist::Show(contact *head)
{
     system("cls");
     Title();
     if(head->next==NULL) 
     {
         cout<<"通讯录为空,请选择操作1插入联系人"<<endl;
         contactlist::Mainscreen(head);
     }
     else
     {
         contact *q;
         q=head->next;
         cout<<"姓名:"<<"      ";
         cout<<"家庭住址:"<<"     ";
         cout<<"电话号码:"<<"     ";
         cout<<"分组:"<<endl;
         while(q!=NULL)
         {
              cout<<q->name<<"          ";
              cout<<q->address<<"           ";
              cout<<q->phonenum<<"           ";
              switch(q->ctype){
              case 1:cout<<"朋友           "<<endl;break;
              case 2:cout<<"同学           "<<endl;break;
              case 3:cout<<"亲戚           "<<endl;break;
              case 4:cout<<"同事           "<<endl;break;
              }
              q=q->next;
         }
         contactlist::Mainscreen(head);
     }
}

void typeshow(contact *head,int choice)
{
    contact *p;
    p=head->next;
    int flag=0;
    while(p!=NULL){
        if(p->ctype==choice){
              cout<<"姓名:"<<"       ";
              cout<<"家庭住址:"<<"      ";
              cout<<"电话号码:"<<"      "<<endl;
              cout<<p->name<<"          ";
              cout<<p->address<<"           ";
              cout<<p->phonenum<<"        "<<endl;
              flag=1;
        }
        p=p->next;
    }
    if(!flag) cout<<"该分组没有记录!"<<endl;
}



void contactlist::TypeShow(contact *head)
{
     system("cls");
     Title();
     if(head->next==NULL) 
     {
         cout<<"通讯录为空,请选择操作1添加记录或6退出系统"<<endl;
         contactlist::Mainscreen(head);
     }
     else
     {
         int choice;
         cout<<"输出所有朋友的记录----------------1"<<endl;
         cout<<"输出所有同学的记录----------------2"<<endl;
         cout<<"输出所有亲戚的记录----------------3"<<endl;
         cout<<"输出所有同事的记录----------------4"<<endl;
         cout<<"请选择:";
         cin>>choice;
         typeshow(head,choice);
         contactlist::Mainscreen(head);
     }
}


void contactlist::Mainscreen(contact *head)
{
     int choice;
     char a='*';
     cout<<endl;
     cout<<" "<<"按照信息后的数字提示选择相应的操作:"<<endl;
     for(int i=0;i<34;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;
     for(i=0;i<34;i++)
     cout<<a<<" ";  
     cout<<endl;
     cout<<"请输入你的选择:";
     cin>>choice;
     switch(choice)
     {
          case 1:contactlist::Assert(head);break;
          case 2:contactlist::Search(head);break;
          case 3:contactlist::Delete(head);break;
          case 4:contactlist::Change(head);break;
          case 5:contactlist::Show(head);break;
          case 6:contactlist::TypeShow(head);break;
          case 7:system("cls");Save(head);exit(0);
     }
}

void contactlist::Title()
{
    char a='*';
    for(int i=0;i<120;i++)
    cout<<a;
    cout<<endl;
    cout<<"                                                通讯录管理系统"<<endl;
    cout<<endl;
    for(i=0;i<120;i++)
    cout<<a;
    cout<<endl;
}




int main()
{
    contactlist *s=new contactlist();
    s->Title();
    cout<<"欢迎进入通讯录管理系统,请选择登陆方式:"<<endl;
    int selectNum;
    cout<<"1 注册新账户"<<endl;//输出一个选择菜单
    cout<<"2 已有的账户登录"<<endl;
    cout<<"3 退出通讯录管理系统"<<endl;
    cin>>selectNum;      
    switch (selectNum)        
    {
          case 1:s->UserReg(s->head);break;        
          case 2:s->Login(s->head);  break;         
          case 3:system("cls");Save(head);exit(0); break;   //退出之前,把改过的通讯录重新读入文档
    }
    return 0;
}
c++
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!