Troubles writing an object to a file.

E

Eddie Fisher

Hello, I am new to this newsgroup. I came here looking for help on an
odd error:

I am currently attempting to make a program that tracks statistics
[manually entered]. It is supposed to write and read objects from a
file.

[ Note: 1.)I got this method from the C++ Black Book by Steven
Holzner (Copyright 2001) ]

Declarations of the ofstream and ifstream objects:
//-------------Code------------------
ofstream pex; // class Player's export stream.
pex.open("database.dat", ios::binary); // Open export stream's
destination

ifstream pin; // class Player's import stream
pin.open("database.dat", ios::binary); // Open import stream's
destination
//-------------End of code section--------------

Active_Player is a Player object. Get_DBNumber() returns an int that
specifies what number the Active_Player is. In the following code, it
(is supposed to) writes the
object to a file.
//-------------Code-------------------
pex.seekp(Active_Player[player_number].Data.Get_DBNumber() *
sizeof(Player), ios::beg);

pex.write(reinterpret_cast<char *> (Active_Player[player_number]),
sizeof(Player));
//-------------End of code section--------------

In the followind code, it (is supposed to) reads the object to a file.
//-------------Code-------------------
pin.seekg(DatabaseSearchNumber * sizeof(Player), ios::beg);

pin.read(reinterpret_cast<char *> (Active_Player[player_number]),
sizeof(Player));
//-------------End of code section--------------

Problem:
error C2440: 'reinterpret_cast' : cannot convert from 'class Player' to
'char *'

I receive this error for both the pin.read() and the pex.write().

When I change it to:

pin.read(reinterpret_cast<Player> (Active_Player[player_number]),
sizeof(Player));

error C2440: 'reinterpret_cast' : cannot convert from 'class Player' to
'class Player'

Upon removing the reinterpret_cast:

pin.read((Active_Player[player_number]), sizeof(Player));

error C2664: 'class istream &__thiscall istream::read(char *,int)' :
cannot convert parameter 1 from 'class Player' to 'char *'

//-----------------------------------------
Any solutions to this problem are welcome. Thanks in advance.
 
V

Victor Bazarov

Eddie said:
Hello, I am new to this newsgroup. I came here looking for help on an
odd error:
[...]

There is nothing odd there. If you think you need to write out
an object as it sits in memory, you need to use the _address_of_
operator:

pex.write(reinterpret_cast<char*>(Active_player + player_number)...

V
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top