save/load problem

This is a discussion on save/load problem within the C++ Programming forums, part of the General Programming Boards category; It's saving to the .txt file, but not loading anything. Code: void SaveGame (void) { ofstream fout; fout.open("XFstats.txt"); fout << ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    249

    save/load problem

    It's saving to the .txt file, but not loading anything.

    Code:
    	void SaveGame (void)
    	{
    	ofstream fout;	
    
    		fout.open("XFstats.txt");	
    				fout << "Do not change or edit the contents of this file. "<<endl<<endl<<endl;
    				fout << kills;
    				fout << "Health: "<< yourhealth;
    				fout << "Weapons: " << crossbow << axe << bronzesword
    					 << silversword << goldsword << platinumsword
    					 << flamethrower << rocketlauncher << endl;
    				fout << "Money: " << money << endl;
    				fout << "Difficulty: " << easy << hard << endl;
    				
    					fout.close();
    					cout<<endl<<"Game saved successfully!"<<endl;
    			getch();
    					repeat();
    	}
    
    
    
    
    	void LoadGame (void)
    	{
    			ifstream fin;
    	char szWord[] = "";
    
    	fin.open("XFstats.txt");	
    
    		if(fin.fail())										
    	{											
    		cout << "ERROR: Could not find XFstats.txt!\n";				
    	}								
    	fin.seekg(NULL, ios::beg);	
    		fin.clear();				
    	fin >> szWord >> kills;
    	fin >> szWord >> yourhealth;
    	fin >> szWord >> crossbow >> axe >> bronzesword
    		>> silversword >> goldsword >> platinumsword
    		>> flamethrower >> rocketlauncher;
    	fin >> szWord >> money;
    	fin >> szWord >> easy >> hard;
    					
    
    fin.close();
    
    cout<<endl<<"Game loaded successfully!"<<endl;
    getch();
    repeat();
    
    
    	}

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    You never allocated any memory for szWord.
    char pszWord[128]; will do it.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    Not only that, but you're overwriting each call by the next. Perhaps you should use separate variables or else use strcat to add each one to the buffer. Better yet, just read in the whole file into a buffer in one call...



    ITSA
    Socket Library!

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    here was my monster save/load function from my old RPG. maybe you can get something from it. i think i did the searching loops pretty clever though since you cant save/load a pointer i had to convert them into integers and convert them back on loading....
    Code:
    case 10:
                ofstream save;
    			ifstream load;
    
    			int choice;
    
    			int weapon_id=0;        //for save function
    	        int scroll1_id=0;
    	        int scroll2_id=0;
    	        int scroll3_id=0;
    	        int room_id=0;
    
    			ROOM* search_room;
    			SCROLL* ptemp_room;
    			WEAPON* ptemp_room1;
    
    			int room_scroll_id;
    
                cout<<"1. Save"<<endl;
    			cout<<"2. Load"<<endl;
    			cout<<"3. Back"<<endl;
    			cout<<"Selection: ";
    
    			cin>>choice;
    
    			if(choice==1)
    			{
    				cout<<endl<<"Saving"<<flush;
    				Sleep(400);
    				cout<<"."<<flush;
    				Sleep(400);
    				cout<<"."<<flush;
    				Sleep(400);
    				cout<<"."<<flush;
    				Sleep(400);
    				cout<<"."<<flush;
    				Sleep(400);
    				cout<<"."<<flush;
    				Sleep(400);
    				cout<<"."<<flush;
    				cout<<"(Hit any key to continue)"<<flush;
    
                    save.open("player.lee", ios::trunc);
    
    				save<<main.name<<"\n";
    				save<<profession<<"\n";
    				save<<main.health<<"\n";
    				save<<main.max_health<<"\n";
    				save<<main.speed<<"\n";
    				save<<main.strength<<"\n";
    				save<<main.magic<<"\n";
    				save<<main.energy<<"\n";
    				save<<main.max_energy<<"\n";
    				save<<main.lvl<<"\n";
    				save<<main.exp_needed<<"\n";
    				save<<main.gold<<"\n";
    				save<<main.scrollcount<<"\n";
    				save<<main.to_hit<<"\n";
    				save<<main.ac<<"\n";
    				save<<level_up_count<<"\n";
    
    				save<<current_weapon->id<<"\n";
    				save<<current_room->id<<"\n";
    
    				if(current_scroll1!=NULL)
    					save<<current_scroll1->id<<"\n";
    				else
    					save<<0<<"\n";
    				if(current_scroll2!=NULL)
    					save<<current_scroll2->id<<"\n";
    				else
    					save<<0<<"\n";
    				if(current_scroll3!=NULL)
    					save<<current_scroll3->id<<"\n";
    				else
    					save<<0<<"\n";
    
    				search_room=&chamber; 
    
    				do
    				{
    				    save<<search_room->flag<<"\n";
    				    search_room=search_room->next;
    				}while(search_room!=&chamber);
    
    				search_room=&chamber;
    				do
    				{
    					ptemp_room=search_room->scroll;
    
    					if(ptemp_room!=NULL)
    					{
    					    room_scroll_id=ptemp_room->id;
    					    save<<ptemp_room->id<<"\n";
    					}
    					else if(ptemp_room==NULL)
    					{
    						save<<0<<"\n";
    					}
    					ptemp_room1=search_room->weapon;
    					save<<ptemp_room1->id<<"\n";
    
    					search_room=search_room->next;
    				}while(search_room!=&chamber);
    			}
    			else if(choice==2)
    			{
    				cout<<endl<<"Loading"<<flush;
    				Sleep(400);
    				cout<<"."<<flush;
    				Sleep(400);
    				cout<<"."<<flush;
    				Sleep(400);
    				cout<<"."<<flush;
    				Sleep(400);
    				cout<<"."<<flush;
    				Sleep(400);
    				cout<<"."<<flush;
    				Sleep(400);
    				cout<<"."<<flush;
    				cout<<"(Hit any key to continue)"<<flush;
    
    				load.open("player.lee");
    
    				int count=1;
    				int id[1000];
    
    				SCROLL* search_scroll;
    				WEAPON* search_weapon;
    
    				load>>main.name;
    				load>>profession;
    				load>>main.health;
    				load>>main.max_health;
    				load>>main.speed;
    				load>>main.strength;
    				load>>main.magic;
    				load>>main.energy;
    				load>>main.max_energy;
    				load>>main.lvl;
    				load>>main.exp_needed;
    				load>>main.gold;
    				load>>main.scrollcount;
    				load>>main.to_hit;
    				load>>main.ac;
    				load>>level_up_count;
    
    				load>>weapon_id;
    				load>>room_id;
    
    				load>>scroll1_id;
    				load>>scroll2_id;
    			    load>>scroll3_id;
    
    				search_room=&chamber; 
    
    				do
    				{
    				    load>>search_room->flag;
    				    search_room=search_room->next;
    				}while(search_room!=&chamber);
    
    				search_room=&chamber; //Loading which scroll IDs are in which rooms
    				do
    				{
    					load>>id[count];
    					count++;
    					load>>id[count];
    					count++;
    					search_room=search_room->next;
    				}while(search_room!=&chamber);
    
    				for(int i=1;i<=(count-1);i+=2) //Scrolls in the rooms
    				{
    					search_scroll=&fireball;
    					do
    					{
    						if(id[i]==search_scroll->id)
    							search_room->scroll=search_scroll;
    						else if(id[i]==0)
    							search_room->scroll=NULL;
    						search_scroll=search_scroll->next;
    					}while(search_scroll!=&fireball);
    					search_room=search_room->next;
    				}
    
    				search_room=&chamber;
    				for(i=2;i<=(count);i+=2)
    				{
    					search_weapon=&dagger;
    					do
    					{
    						if(id[i]==search_weapon->id)
    							search_room->weapon=search_weapon;
    						search_weapon=search_weapon->next;
    					}while(search_weapon!=&dagger);
    					search_room=search_room->next;
    				}
    
    				search_weapon=&dagger;
    				do
    				{
    					if(search_weapon->id==weapon_id)
    						current_weapon=search_weapon;
    					search_weapon=search_weapon->next;
    				}while(search_weapon!=&dagger);
    
    				search_room=&chamber;
    				do
    				{
    					if(search_room->id==room_id)
    						current_room=search_room;
    					search_room=search_room->next;
    				}while(search_room!=&chamber);
    
    				search_scroll=&fireball;
    				do
    				{
    					if(search_scroll->id==scroll1_id)
    						current_scroll1=search_scroll;
    					if(search_scroll->id==scroll2_id)
    						current_scroll2=search_scroll;
    					if(search_scroll->id==scroll3_id)
    						current_scroll3=search_scroll;
    					search_scroll=search_scroll->next;
    				}while(search_scroll!=&fireball);
    			}
    		}

  5. #5
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361

    sleep and loading

    Can anyone here give an example, a complete example on how to use sleep properly, and, a full example, with comments on how to load a file? thx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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