保存文件中包含字符串的对象

时间:2016-11-21 03:24:50

标签: c++

当我运行此程序时,读取文件编译器出错时显示访问冲突错误请帮助我解决这个问题我已尝试<<>>运算符进行写入和阅读但我正在使用由于我不能使用这些运算符的下一个指针

#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;
class employee
{ private:


   int id;
   static int count;

public:
    string name;
    employee *next;

    employee()
    { cout<<"enter name:";
      cin>>name;
      count++;
      id=count;
      next=NULL;
    }
    employee(int x)
    {}
    void setcount(int x)
    {count=x;}
    void show()
    {cout<<"Name: "<<name<<endl;
     cout<<"id: "<<id<<endl;
    }
    void setname(string s)
    {name=s;}

    string getname()
    {return name;}

    int getid()
    {return id;}
 };
int employee::count=0;

class db
{  employee *list;

public:
  db ()
  {  list=NULL;   }

  void hire()
  {employee *e=new employee;
   employee *temp=list;

  if(list==NULL)
  {list=e;}
  else{
  while(temp->next!=NULL)
   { temp=temp->next; }
  temp->next=e;
  }//else end
  cout<<"hired..."<<endl;
  }

  void displayall()
  { if(list==NULL)
    cout<<"NO record..";
  else
  {  employee *temp=list;
      while(temp!=NULL)
      {temp->show();  
       temp=temp->next;  }
    }//else  end
  }

  void remove()
  {  int id;
     int found=0;
    cout<<"Enter id to be removed...";
     cin>>id;
     if(list==NULL)
         cout<<"no record..";
     else
     {  employee *temp=list;
        employee *pre=list;
       while(temp!=NULL)
         { if( temp->getid()==id)
            {   found=1;
             employee *e=temp;
             e->show();
            if(temp==list) //first item to b removed
             list=list->next;
            else
             pre->next=temp->next;
         delete e;  cout<<"record deleted"<<endl;
         break;
          }
         pre=temp;
         temp=temp->next;  
        }//while end
     if(found==0)
     cout<<"record not found.."<<endl;
     }//else end
  }

  void searchbyname()
  {  string name;
     int found=0;
     cout<<"enter name..";
     cin>>name;

   if(list==NULL)
         cout<<"no record..";
     else
     { employee *temp=list;
         while(temp!=NULL)
           { if( temp->getname()==name)
               {   found=1;
                temp->show();}
             temp=temp->next;  
           }//while end
     if(found==0)
     cout<<"record not found.."<<endl;
     }//else end
  }

  void save()
{ ofstream out;
  out.open("eb",ios::out | ios::binary);
  if(!out)
      cout<<"cannot save..";
  else
   {  employee *temp=list;
      while(temp!=NULL)
     {  out.write((char*) temp,sizeof(employee));
         temp=temp->next;
      }
      out.close();
   }

  }

  void retrieve()
  { ifstream in;
  in.open("eb",ios::in | ios::binary);
  if(!in)
      cout<<"cannot retrieve..";
  else
  { employee *temp=NULL;
  employee e(0);
     while(1)
     { in.read((char*) &e,sizeof(employee));
       employee *p=new employee(e);  //dynamically creating an employee that will have the same value as 'e' and saving its address in p. Every node in link list has different location so p will be different. &e is always same. So actually read node from file in e than create a node dynamically with same contents and address will be save in p.
     if(list==NULL)
            {list=p;
             temp=list;
     p->show();}
         else{
             temp->next=p;
              temp=temp->next;
               e.show();
              if(e.next==NULL)
              { e.setcount(e.getid()); break;}          
              } 

       }
      in.close(); 
     }


  }
};







void main()
{ 
 db obj;
try{ obj.retrieve();

 int op;
 while(1)
 { cout<<endl<<"Enter 1 to hire ,2 for remove ,3 for displayall, 4 for search by name.. 5 for exit";
    cin>>op;
    switch(op)
    {case 1:    
     obj.hire(); break;
    case 2:
        obj.remove(); break;
    case 3: 
        obj.displayall();  break;
    case 4:
        obj.searchbyname(); break;
    }
    if(op==5)
        break; 
  }
 obj.save();
}
catch(...)
{cout<<"error..";}
 getch();
}

1 个答案:

答案 0 :(得分:0)

您无法使用以下代码从文件中读取您的班级数据:in.read((char*) &e,sizeof(employee));您的班级包含std::string和指针,它们将由值初始化,这一点无处可寻。任何使用它们的尝试都是未定义的行为,导致你的AV。