Thread: How to store an array in a file?

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    6

    How to store an array in a file?

    Okay, I have a game with a structure that holds all the player information. I've figured out how to save and load the basic variable types, but I've run into trouble with an array I've added to the player structure. Here's the code I use to save:

    Code:
    void CFightOMaticDlg::SaveGame()
    {
    	
    	
    	UpdateData(TRUE);
    
    	CFile file(_T(m_sPlayer_Name), CFile::modeCreate|CFile::modeWrite);	
    	CArchive ar(&file , CArchive::store);
    
    	ar << m_sPlayer_Name << Player.m_iHealth << Player.m_iHealth_Max << Player.m_iGold << Player.m_iExperience
    		<< Player.m_iPlayer_Level << Player.m_iStrength << Player.m_iDefense << Player.m_sWeapon_Name << Player.m_iWeapon_Damage
    		<< Player.m_sArmor_Name << Player.m_iArmor_Damage << Player.m_iWeapon_Damage_Roll << Player.m_bPlayer_Can_Camp
    		<< Player.m_iaPlayer_Inventory_Last_Spot;
    	ar.Close();
    	file.Close();
    	
    }
    And the code for loading is of course pretty much the same. The variable I need to store is called Player.m_iaPlayer_Inventory[]

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    A simple solution would be to output the number of items in the array and then output each element. That way when you are loading you will know how many elements to read right away.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    6
    I'm not sure what you mean by output,
    the elements in the array I want to save hold an int that is the id of an item structure.
    The variable Player.m_iaPlayer_Inventory_Last_Spot is an int that tells me the first empty spot in the array.
    The maximum elements that could be in the array is 11. I use this information to fill a ListBox in the rest of the program.
    Last edited by Burlock; 12-14-2003 at 02:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM