Save class

not found save string.

#include <iostream>
#include <fstream>

using namespace std;


class person{
int id;
string name;
public:
void setId(int id);
void setName(string name);

int getId() const;
string getName() const;

void savefile(person save);
void openfile();

};

void person::setId(int id)
{
this->id =id;
}

void person::setName(string name)
{
this->name = name;
}

int person::getId() const
{
return this->id;
}

string person::getName() const
{
return this->name;
}

void person::savefile(person save)
{
fstream *ffile = new fstream;
ffile->open("clase.txt",ios::out | ios::app | ios::binary);
ffile->write( (char*)&save,sizeof(person));
ffile->close();
delete ffile;
}

void person::openfile()
{
person view;
fstream *ffile = new fstream;
ffile->open("clase.txt", ios::in | ios::binary);
if(ffile->fail())
cerr << "Error al abrir." << endl;
else
while(!ffile->eof())
{
ffile->read((char*)&view,sizeof(person));

if(!ffile->eof())
{
cout << "\nId: " << view.getId();
cout << "\nNombre: " << view.getName();
}
}
ffile->close();
delete ffile;

}

int main()
{
person *per = new person;
per->setId(1);
per->setName("Hello");
cout <<"\n====\n";
cout << "\nId: " << per->getId();
cout << "\nName: " << per->getName();
cout <<"\n====save file ====\n";
per->savefile(*per);
delete per;

person *ppp = new person;
cout <<"\n====Open file ====\n";
per->openfile();
delete ppp;
return 0;
}
not found save string


Care to elaborate?

edit: you need
#include <string>

as well.
Last edited on
Topic archived. No new replies allowed.