Like Tree1Likes
  • 1 Post By oogabooga

Using binary write to save a struct

This is a discussion on Using binary write to save a struct within the C++ Programming forums, part of the General Programming Boards category; Hi I'm still reading Prata's book and there is a section where they explain how to write files using the ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    10

    Using binary write to save a struct

    Hi


    I'm still reading Prata's book and there is a section where they explain how to write files using the binary mode.


    They define a struct


    Code:
    const int LIM = 20;
    struct planet
    {
    char name[LIM]; 
    double population;
    double g; 
    };
    planet pl;

    And they say you can use the binary mode to write the whole struct at once (in text mode you need to specify every member).

    Code:
    ofstream fout("planets.dat",
    ios_base:: out | ios_base::app | ios_base::binary);
    fout.write( (char *) &pl, sizeof pl);
    I don't understand why they use
    Code:
    fout.write( (char *) &pl, sizeof pl)
    instead of just
    Code:
    fout.write( pl, sizeof pl)

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,322
    I would assume that fout.write() requires a (char *) parameter for the buffer address. (void *) may also work.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Firstly, passing pl wouldn't work at all since that would pass all the data in the object. You need to pass in the address of the data, not the data itself; hence the &.

    The (char*) cast (or a more appropriate C++ style cast) is necessary to match the address types. The write() method knows nothing of your object and just treats it as a sequence of bytes. A sequence of bytes is essentially a char array; hence the (char*).
    skan likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    10
    Quote Originally Posted by oogabooga View Post
    Firstly, passing pl wouldn't work at all since that would pass all the data in the object. You need to pass in the address of the data, not the data itself; hence the &.

    The (char*) cast (or a more appropriate C++ style cast) is necessary to match the address types. The write() method knows nothing of your object and just treats it as a sequence of bytes. A sequence of bytes is essentially a char array; hence the (char*).
    Good explanation

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,901
    I would recommend you avoid Stephen Prata's books (they are not good).
    This is a good list of recommended books: C++ Book Recommendations
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-09-2008, 08:27 AM
  2. how to write save code?
    By sept in forum C++ Programming
    Replies: 6
    Last Post: 09-18-2007, 08:25 AM
  3. Still can not write or read from save file
    By WackoWolf in forum C++ Programming
    Replies: 37
    Last Post: 11-14-2004, 07:28 PM
  4. Still can not write or read from save file
    By WackoWolf in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2004, 01:21 AM
  5. how to write and save ini files like some apps do with their ini's
    By Mouse_103 in forum Windows Programming
    Replies: 1
    Last Post: 12-19-2003, 12:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21