Question about binary file streaming

This is a discussion on Question about binary file streaming within the C++ Programming forums, part of the General Programming Boards category; outfile.open(“data.txt”, ios::binary) outfile.write(reinterpret_cast <char*> (&Object_name), sizeof (Object_type) ); I know you can use this command. I need to save a ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    57

    Question about binary file streaming

    outfile.open(“data.txt”, ios::binary)
    outfile.write(reinterpret_cast <char*> (&Object_name), sizeof (Object_type) );

    I know you can use this command. I need to save a couple of classes to a binary how would i do this. Fir example it is a team roster and i need to save each player in binary and the player information. How would i got upon doing that. Plz just explain don't right the program out. Thanks

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Use what command? The write member function for the ofstream class? Yes you can.

    You've got what you need basically although you need an extra piece in the open call.
    Code:
    outfile.open(“data.txt”, ios::binary | ios::out)
    outfile.write(reinterpret_cast <char*> (&Object_name), sizeof (Object_type) );
    That should work (to save a single object to a file) provided the object in question does not have any pointers to dynamically allocated objects of its own (arrays) or STL containers of any kind, i.e. string's, vector's, etc... or is a composite object that may contain sub-objects that do have such elements in them. If it does, then you will need to take special considerations when writing the object such that you can properly read the object back from the file when the time comes. The logical "size" of such objects will not be simply sizeof(Object_type) but something more complex and varied. (It would help to see what your "object" looks like.)

    If you need to save more than one object, then presumably you would have a container (array) of such objects. You would need to open your file as you do above and then loop/iterate through the container and perform the write call for each element in said container.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Reading a file in binary question
    By Dan17 in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 12:28 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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