Troubles writing an object to a file.

Discussion in 'C++' started by Eddie Fisher, Jul 28, 2004.

  1. Eddie Fisher

    Eddie Fisher Guest

    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! =-----
     
    Eddie Fisher, Jul 28, 2004
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Jul 28, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HNguyen
    Replies:
    4
    Views:
    2,452
    HNguyen
    Dec 21, 2004
  2. PeterKellner
    Replies:
    2
    Views:
    535
    PeterKellner
    Jun 10, 2006
  3. Vladimir Ignatov

    Troubles with CSV file

    Vladimir Ignatov, May 14, 2004, in forum: Python
    Replies:
    5
    Views:
    370
    Skip Montanaro
    May 14, 2004
  4. Vladimir Ignatov

    Re: Troubles with CSV file

    Vladimir Ignatov, May 17, 2004, in forum: Python
    Replies:
    2
    Views:
    259
    Fuzzyman
    May 17, 2004
  5. David Buchan
    Replies:
    18
    Views:
    686
    Flash Gordon
    Dec 22, 2004
Loading...

Share This Page