saving object having string in file


When i am running this program having eroor in reading file please help me out
#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 reterive()
{ ifstream in;
in.open("eb",ios::in | ios::binary);
if(!in)
cout<<"cannot reterive..";
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.reterive();

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();
}
The code is good so it must be your file or path.
no t is due to the pointer used in string but i am not getting what alternative i can use
The simplest alternative is to open the file in text mode instead of binary mode.
Then output each member variable id and name as ordinary text, using suitable delimiters. For example, use '\t' to separate each variable and '\n' to mark the end of each record.
i understood your first point but didn't get your second point
How you store the data in the file is up to you. The main thing is to output the data it in a suitable way that can later be read back in.
I am not getting your point that how I output data in a suitable way
Well, I'm not sure it was that important, maybe this is distracting from the real issue which is this: are you now able to go ahead and modify the program so that it works as required, or not? If not, where are you stuck.
i am getting problem in retrieve function when all objects have read from file than it shows error of access of violation
ok so all I did was make sure it would open the file.

Is it reading the entire file then giving a error ? If so then maybe there is a space or something at the end of the file it does not like.
Topic archived. No new replies allowed.