Thread: Read/Write Binary Files

  1. #1
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312

    Read/Write Binary Files

    I want to read/write a binary file (I'm making a game, text-based for now, graphics later) I'm using this code for reading/writing:

    Code:
    void player::save()
    {
    	cout << "Saving...";
    	ofstream save;
    	save.open("bin.dat", ios_base::binary);
    	if(!save.is_open())
    	{
    		cerr << "Error in file I/O";
    		exit(1);
    	}
    
    	save.write(reinterpret_cast<char *> (this), sizeof(this));
    	save.close();
    }
    
    void player::load()
    {
    	player b;
    	ifstream load;
    	load.open("bin.dat", ios_base::binary);
    	if(!load.is_open())
    	{
    		cerr << "Error in file I/O";
    		exit(1);
    	}
    
    	load.read(reinterpret_cast<char *> (&b), sizeof(&b));
    
    	cout << "Loaded: " << b.name;
    	load.close();
    }
    I took this code from my book (2 chapters ahead from my current chapter), but it doesn't work, no errors/warnings/runtime problems, but the program doesn't save the info. When I execute load() in my program (after save(), the file exists) it couts only "Loaded: " and not the b.name, is this a reading or writing problem and how can I solve it?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I guess that Player.name is a std:string. So what you are saving with write is only the string object and not it's data.
    You should search for serialisation.
    There are many serialization libraries available.
    e.g. boost/serialization
    Kurt

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>save.write(reinterpret_cast<char *> (this), sizeof(this));

    this is a pointer, and the size of any pointer is a constant value, 4 on 32-bit compilers. add the asterisk (star) in front of this to get its size
    Code:
    save.write(reinterpret_cast<char *> (this), sizeof(*this));
    That will write the class, but if the class contains std::string or any other std:: containers that line will not write them out. Each of those objects have to be written out individually, which might take a little pre-planning on your part.

  4. #4
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    I think I'm not going to use binary storage, I'll just use the overloaded >> and << operators. Can I use the encryption algorithm of the code snippets for encryption? (Against cheaters :P)
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I just made my own simple encryption, I made it so any character you type is the opposite, like, if you type A, and save it. It is saved as Z, or 0 is 9, and so on. THat will work for most cheaters, but hackers it doesnt matter what you do they can crack it.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Writing variable length strings to disk is a pain in the arse b/c you can't just stick the data in a structure and do a block read/write. You can either overload operators to work with string data types or you can specify the length of the string in a member and then read in that many bytes.

    MFC's CString and CArchive are extremely handy as they let you use insertion and extraction operators on all standard data types and on CStrings. A CString object knows how to read and write itself to/from disk. You may be able to use these by simply linking with the MFC DLL as a shared DLL. You do not have to have a complete MFC program to use the utilities in MFC.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    According to the poster's sig, his OS is SUSE, and his compiler is g++. I'm guessing he is going to run into problems attempting to use MFC.

  8. #8
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Oops, I need to change my sig.. I use Ubuntu now, and windows works again! :P
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well since I don't even know what the hell Ubuntu is, I'd say switch OS's.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Bubba
    Well since I don't even know what the hell Ubuntu is, I'd say switch OS's.

    google

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Strange name.

  12. #12
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    It's the easiest linux distro (they say: "Linux for human beings") It is very easy, and has a lot of windows things working. "Ubuntu" means something like "Being humain to other people", and "I am what I am by what we are" in old-African (Translated from Dutch ).
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Copying Binary Files
    By mikeman118 in forum C++ Programming
    Replies: 9
    Last Post: 08-11-2007, 10:55 PM
  3. Processing binary or plaintext files
    By Jags in forum C Programming
    Replies: 12
    Last Post: 08-04-2006, 02:35 PM
  4. send/recv binary files using sockets in C++
    By dafatdude in forum Networking/Device Communication
    Replies: 14
    Last Post: 07-25-2004, 11:00 AM