A
Andreas Palsgård
Hey there...
I hope someone can help me with my problem.
I want to write a class object with both strings and integers, to a file,
and be able to read it properly again. I thought that the code below worked
(because it appears as so in a c++ book), but it does not. Can you make it
work? =)
thanx
Code:
--------
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
class person
{
protected:
string name,colour;
int age;
public:
void getdata()
{
cout << "Enter name:" ; cin >> name;
cout << "Enter Age:"; cin >> age;
cout << "Enter colour:"; cin >> colour;
}
void showdata()
{
cout << "Name: " << name << endl;
cout << "Age:" << age << endl;
cout << "Colour:" << colour << endl;
}
string get_name(){ return name;}
int get_age(){ return age;}
string get_colour(){ return colour;}
};
person tim;
void file_in(){
person temp_pers;
ifstream infile("person.dat", ios::in | ios::binary);
infile.read(reinterpret_cast<char*>(&temp_pers),sizeof(temp_pers));
infile.close();
tim = temp_pers;
cout << temp_pers.get_name() << endl;
cout << temp_pers.get_age() << endl;
cout << temp_pers.get_colour() << endl;
}
void file_out(){
tim.getdata();
person temp_pers;
temp_pers = tim;
ofstream outfile("person.dat", ios::binary | ios:ut | ios::trunc);
outfile.write(reinterpret_cast<char*>(&temp_pers),sizeof(temp_pers));
outfile.close();
cout << temp_pers.get_name() << endl;
cout << temp_pers.get_age() << endl;
cout << temp_pers.get_colour() << endl;
}
/////////////////////////////
int main()
{
file_out();
//file_in();
return 0;
}
I hope someone can help me with my problem.
I want to write a class object with both strings and integers, to a file,
and be able to read it properly again. I thought that the code below worked
(because it appears as so in a c++ book), but it does not. Can you make it
work? =)
thanx
Code:
--------
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
class person
{
protected:
string name,colour;
int age;
public:
void getdata()
{
cout << "Enter name:" ; cin >> name;
cout << "Enter Age:"; cin >> age;
cout << "Enter colour:"; cin >> colour;
}
void showdata()
{
cout << "Name: " << name << endl;
cout << "Age:" << age << endl;
cout << "Colour:" << colour << endl;
}
string get_name(){ return name;}
int get_age(){ return age;}
string get_colour(){ return colour;}
};
person tim;
void file_in(){
person temp_pers;
ifstream infile("person.dat", ios::in | ios::binary);
infile.read(reinterpret_cast<char*>(&temp_pers),sizeof(temp_pers));
infile.close();
tim = temp_pers;
cout << temp_pers.get_name() << endl;
cout << temp_pers.get_age() << endl;
cout << temp_pers.get_colour() << endl;
}
void file_out(){
tim.getdata();
person temp_pers;
temp_pers = tim;
ofstream outfile("person.dat", ios::binary | ios:ut | ios::trunc);
outfile.write(reinterpret_cast<char*>(&temp_pers),sizeof(temp_pers));
outfile.close();
cout << temp_pers.get_name() << endl;
cout << temp_pers.get_age() << endl;
cout << temp_pers.get_colour() << endl;
}
/////////////////////////////
int main()
{
file_out();
//file_in();
return 0;
}