By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

HELP: Class object I/O

P: n/a
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),si zeof(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::out | 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;
}
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Andreas Palsgård" <12*****@get2net.dk(fjern 123)> wrote in message news:3f***********************@dread11.news.tele.d k...
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.
Jul 19 '05 #2

P: n/a
Andreas Palsgård wrote:
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...

Jul 19 '05 #3

P: n/a
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.
"Andreas Palsgård" <12*****@get2net.dk(fjern 123)> wrote in message
news:3f***********************@dread11.news.tele.d k...
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),si zeof(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::out | 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;
}

Jul 19 '05 #4

P: n/a
klaas wrote:
Andreas Palsgård wrote:
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

Jul 19 '05 #5

P: n/a
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

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f*********************@news.newshosting.com. ..

"Andreas Palsgård" <12*****@get2net.dk(fjern 123)> wrote in message news:3f***********************@dread11.news.tele.d k...
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.

Jul 19 '05 #6

P: n/a
Andreas Palsgård wrote:
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.


Andreas


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

Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.