Thread: file i/o, struct questions

  1. #1
    Registered User
    Join Date
    May 2008

    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.
    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.

    struct saveGame
    	player savep1;
    	player savep2;
    	player saveCurr;
    	player saveOpp;

    here's the save function:

    void save()
    	saveGame save;
    	save.savep1 = p1;
    	save.savep2 = p2;
    	save.saveCurr = current;
    	save.saveOpp = opponent;
    	ofstream outputStream("saveBattle.dat", ios::out | ios::binary);
    		cout << "Failed to open file";
    	outputStream.write((char *) &save, sizeof(saveGame));
    	cout << "Game has been saved";
    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

    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,


  2. #2
    The larch
    Join Date
    May 2006
    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:

    (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:
    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-18-2005, 12:09 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    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, 02:58 AM