Object Serialization C++
Jul 25, 2011 at 12:29am UTC
Hi there!
I'm trying to serialize some of my objects, but when they're pointers I don't know how to do it... I have being studying this code that I found in internet and I can understand it (I think), but I don't know why when I'm using pointers to objects I can't make it work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <fstream>
#include <iostream>
using namespace std;
class person{
protected :
char name[80];
int age;
public :
void getData(){
cout << "\n Enter name: " ; cin >> name;
cout << " Enter age: " ; cin >> age;
}
void showData(){
cout << "\n Name: " << name;
cout << "\n Age: " << age;
}
};
int main(){
char ch;
//preson pers;
person *pers = new person; // pers now is a pointer
fstream file;
file.open("GROUP.DAT" , ios::app | ios::out | ios::in );
do {
cout << "\nEnter person's data:" ;
pers->getData();
//file.write( reinterpret_cast<char*>(&pers), sizeof(pers) ); //No pointer
file.write( reinterpret_cast <char *>(pers), sizeof (pers) ); //Pointer
cout << "Enter another person (y/n)? " ;
cin >> ch;
}while (ch=='y' );
file.seekg(0);
//file.read( reinterpret_cast<char*>(&pers), sizeof(pers) ); //No pointer
file.read( reinterpret_cast <char *>(pers), sizeof (pers) ); //Pointer
while ( !file.eof() ){
cout << "\nPerson:" ;
pers->showData();
//file.read( reinterpret_cast<char*>(&pers), sizeof(pers) ); //No pointer
file.read( reinterpret_cast <char *>(pers), sizeof (pers) ); //Pointer
}
cout << endl;
return 0;
}
The problem is that at the end always give me the last name with the last age that I have introduced, but when I'm not using a pointer that doesn't happen... Why could be this happening?
Jul 25, 2011 at 1:25am UTC
I've made a simple example, it loads/saves one person and an array of people.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include <string>
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <fstream>
using namespace std;
class Person {
public :
char name[80];
int age;
Person(){}
Person(const char * n, int age): age(age)
{
strcpy(name, n);
}
};
int main ()
{
const char * FILENAME = "Z:\\Temp\\file.txt" ;
const int NUM = 10;
ofstream outfile;
ifstream infile;
//Save person
Person* person1 = new Person("Joe" , 10);
outfile.open(FILENAME, ofstream::trunc);
outfile.write((char *)person1, sizeof (Person));
outfile.close();
//Load person
Person* person2 = new Person();
infile.open(FILENAME);
infile.read((char *)person2, sizeof (Person));
infile.close();
cout << person2->name << endl;
//Save array
Person* persons = new Person[NUM];
for (int i = 0; i < NUM; i++)
itoa(i, &persons[i].name[0], 10); //Just assign number as each person's name
outfile.open(FILENAME, ofstream::trunc);
outfile.write((char *)persons, sizeof (Person) * NUM);
outfile.close();
//Load array
Person* persons2 = new Person[NUM];
infile.open(FILENAME);
infile.read((char *)persons2, sizeof (Person) * NUM);
infile.close();
cout << persons2[5].name << endl;
return 0;
}
Last edited on Jul 25, 2011 at 1:43am UTC
Topic archived. No new replies allowed.