HELP: Class object I/O

  • Thread starter Andreas Palsgård
  • Start date
A

Andreas Palsgård

Hey.. i want to write a class object containing both strings and integers,
to a file on disk, and be able to read it properly again at another time. I
made the code below... it doesn't work, but what am i doing wrong.

thanx
Andy
-----------------------------------
#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::eek: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;
}
 
R

Ron Natalie

Andreas Palsgård said:
Hey.. i want to write a class object containing both strings and integers,
to a file on disk, and be able to read it properly again at another time. I
made the code below... it doesn't work, but what am i doing wrong.
You posted this already. You can not write out most classes (other than
the most simple) by casting their pointer to char* and calling Write.
Your book, if it actually says to do that, is worse than useless. Burn it.
The problem is that classes (such as string) may contain pointers to
data elsewhere that is neither written by your hack nor properly restored
when read in. Further you may stomp on C++ overhead for things like
virtual functions that you have no business accessing.
 
K

klaas

Andreas said:
Hey.. i want to write a class object containing both strings and integers,
to a file on disk, and be able to read it properly again at another time. I
made the code below... it doesn't work, but what am i doing wrong.
the problem is:
you don't know the size of the data you are writing to the file in advance.
You have to make any char * into a char[fixed_size]
to be able to let any size_of(your_object) return a descent value.
that way your reinterpret_cast<hex> won't fail....

so no char * pointers please...
 
M

Moonlit

Hi,

Object serialization is a great thing, unfortunately there is no magic
behind it. Do the following:

Define a function (possibly operator<< or serialize with in = false ) to
write every value of the class to disk.

Define a function ( possibly operator>> or serialize ( with in = true) to
read every value from disk.

For aggregated classes call the serialize member with the "in" or the
operator>> or operator<< parameter.

This will cause a tree of serialization to occur If every class knows how to
correctly write and read its values it will succeed.

In a few words, every object is responsible for reading or writing itself to
persistent storage.

Regards, Ron AF Greve.
 
K

klaas

klaas said:
Andreas said:
Hey.. i want to write a class object containing both strings and
integers,
to a file on disk, and be able to read it properly again at another
time. I
made the code below... it doesn't work, but what am i doing wrong.
the problem is:
you don't know the size of the data you are writing to the file in advance.
You have to make any char * into a char[fixed_size]
to be able to let any size_of(your_object) return a descent value.
that way your reinterpret_cast<hex> won't fail....

so no char * pointers please...
If you have the fixed size issue ready i can give you a wrapper class
that implement harddisk based arrays...
let me know
 
A

Andreas Palsgård

well... actually i found this out:
My little program deosn't work with strings... but if you convert strings to
char[] it works perfectly.
You can write the whole object to a file with reinterpret_cast[char*]...

Andreas
 
T

Thomas Matthews

Andreas said:
well... actually i found this out:
My little program deosn't work with strings... but if you convert strings to
char[] it works perfectly.
You can write the whole object to a file with reinterpret_cast[char*]...

You _can't _ use reinterpret_cast<> on a string. It is a complex class
and not a simple type. Use the string::c_str() method for conversion
to an array of characters (C-style string) and a constructor for
converting from a C-style string to a std::string.


1. Don't Top-Post (replies are appended to the bottom, or interspersed
throughout).
2. Strings are known as variable record types when serializing.
(See also C++ faq about serializing).
You have to decide whether you want to use the
[quantity][text] or [text][sentinel]
layout.
3. Strings are one of those types that is best serialized in one
form, but used internally in another. Endianism is another
of these same issues.

In my Binary_Stream class, I have chose the [quantity][text]
layout. I also have every class write out their own members.



--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top