473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Troubles writing an object to a file.

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.

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #1
1 1884
Eddie Fisher wrote:
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
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
3
by: Keith | last post by:
I am using the built-in setup installer which comes with VS 2003. I am always having problems running the application on other machines other than my development machines with VS. It seems like...
9
by: Kelly Vernon | last post by:
I have a standard ASP page that appends to an xml page. Currently if there is more than one person attempting to append to the same XML file at a time. One user will have the ability to append,...
3
by: John | last post by:
We have a DW and are using TSM for the logs and backups. We are constantly getting retry messages in the db2diag.log inidicating that DB2 won't try to archive for another 5 minutes. This is a...
3
by: Mike Dundee | last post by:
I am importing data into a new database (the database still has to be set up) and have a problem. The comma delimited text files I am importing have four fields containing date and date/times. ...
0
by: Stefan Slapeta | last post by:
Hi all, I've experienced some troubles with message tables and wanted to know if anybody knows a solution for one of them: - If I translate my .mc file into a Unicode .bin file, some of the...
2
by: Jolly Green Giant | last post by:
I'm trying to use PHP to create a text file. Simple right? I've done it before. I don't know why it's not working now. It's not rocket science. In general, I'd say I'm a PHP beginner, but with...
6
by: bonk | last post by:
I am trying to create a stream that writes text to a file and: - automatically creates a new file once the current file exceeds a certain size - makes it possible to be used by multiple threads...
2
by: Joe324 | last post by:
Hello, I'm writing a program that will (eventually) read in a list of names from a file and store them in a linked list. I have the basics of the program done and it compiles with no errors, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.