file i/o, struct questions

This is a discussion on file i/o, struct questions within the C++ Programming forums, part of the General Programming Boards category; Hi all. I'm writing a battleships type game. Each player is represented as a struct, which contains 2 10 x ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    7

    file i/o, struct questions

    Hi all.

    I'm writing a battleships type game. Each player is represented as a struct, which contains 2 10 x 10 arrays, player's name, etc.
    Code:
    struct player
    {
    	char shipBoard[10][10];
    	char bombBoard[10][10];
    	string name;	
    	int shipRem;
    };
    There are 2 players, but there are 4 instances of player, so that I can swap players, called p1, p2, current & opponent.

    I want to be able to save the game, so I've created another struct, which contains 4 instances of player, called saveGame.

    Code:
    struct saveGame
    {
    	player savep1;
    	player savep2;
    	player saveCurr;
    	player saveOpp;
    };

    here's the save function:

    Code:
    void save()
    {
    	saveGame save;
    
    	save.savep1 = p1;
    	save.savep2 = p2;
    	save.saveCurr = current;
    	save.saveOpp = opponent;
    	ofstream outputStream("saveBattle.dat", ios::out | ios::binary);
    	if(!outputStream)
    	{
    		cout << "Failed to open file";
    		exit(1);
    	}
    	outputStream.write((char *) &save, sizeof(saveGame));
    	outputStream.close();
    	cout << "Game has been saved";
    	exit(0);
    }
    I have a restore function which is similar, only in reverse.

    So I have a struct that contains structs.

    I then write that struct to a file, and read it to restore the game, using a similar process.

    What I want to know is do I have to specifically use a loop process, to put the data back into the 10 x 10 matrices, for each player? Ive tried just using the call

    Code:
    p1 = restore.savep1;
    ans so on for each player, and then continuing the game, but it doesn't seem to work.

    Sorry if what I have written isn't clear. It's quite hard to explain!

    Thanks in advance,

    Justin

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Firstly, why not provide a save/load or write/read member for the player class. Then you could simply save the four players one after another and read them in the same order:

    Code:
    p1.save(outputStream);
    p2.save(outputStream);
    current.save(outputStream);
    opponent.save(outputStream);
    (In case you have more than one player, it might be better to use an array/container of them, so tasks like that could be handled in a loop.)

    And secondly:
    Code:
    outputStream.write((char *) &save, sizeof(saveGame));
    This doesn't work for any non-POD types (in your case the structs contain std::string which makes them more than plain-old-data): std::string contains pointers, so you'd be saving random memory addresses and not the contents (the string itself) found at that address.

    It is probably easier to save data in text mode than binary mode.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-17-2005, 11:09 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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