Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Aha, workable solution to saving game files!!!

Got an idea for a practice project / debug snippet / short-code contest / etc? Tell us about it here!

Aha, workable solution to saving game files!!!

Postby Doctor Salt on Fri Aug 22, 2008 2:31 pm

From a whole lot of research on the internet, and this one little book I have, i have found out a great way to "save" games, using files.

As antiRTFM probably already knows...... You can use filestreams, to write, and read, to txt files.

For instance, heres something i coded, that asks you to enter in two lines of text. It then takes that text, puts it into a file named 'awesome'. If there is already a folder with that name, it deletes everything inside it, and puts in your new text. If it doesn't exist, it creates one. The "awesome" txt folder is located in....

C:\Users\username\Documents\VisualStudio2008
\Projects\your project name\your project name/awesome

Then, under the reading function, it opens the file up again, and shows you whats inside the awesome.txt.

So i hope this could help people.... I'm going to quickly follow up on a more indepth description of what the code does for anyone that wants to know...

One thing about my code, I added in the paremeters "ios::trunc" to overwrite any old files/create new ones. The truncate mode should be default, so you needn't put that in.

(P.S At the moment, I am trying to figure out how to read the txt folder, and assign variables from that, like variable 1=line 1 of the folder, type thing. Once i do, i can easily create a save system, which can have multiple save spots, then my specified save )


And heres the code

Code: Select all
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
#include <windows.h>
using namespace std ;


int reading();
void wait ();
void sleep( unsigned int uSeconds );


int main()
{
string a;
string b;

cout << "Enter in your first line, then second line, etc.\n\n";
getline( cin, a);
   a.append ("\n");
getline( cin, b);
   b.append ("\n");

;
   cout << "Now i will write to the file, 'awesome'\n\n\n";
   Sleep(2000);

   string test = a;
   test.append( b ) ;

   cout << "\nWriting done!";

   ofstream writer( "awesome.txt", ios::trunc) ; //making the file (truncate is what deletes the old files and starts new)

   writer << test << endl; //assigning my string, test, to the file

   cout << "\nhokay, it should've worked\n\n";
   Sleep(2000);

   if( ! writer )   //this checks if it actually created the file
   {
      cout << "Error opening file for output" << endl ;
      Sleep(2000);
      return -1 ;
   }
   wait ();
   writer.close() ;

   reading ();
      return 0 ;
}


int reading() //This reads the file
{
   int i ;
   string line ;
   cout << "\n In the reader function\n";
   Sleep(2000);
   ifstream reader( "awesome.txt", ios::in ) ;  //reads the file

   if( ! reader )
   {
      cout << "Error opening input file" << endl ;
      Sleep(2000);
      return -1 ;
   }
   else
   for( i = 0; ! reader.eof() ; i++ ) //reads untill the end of file, and counts how many lines it goes through
   {
      getline( reader , line ) ;
      cout << line << endl ;
   }

   reader.close() ;
   
   cout << "Iterations: " << (i/2) << endl ;
   Sleep(2000);
   cout << "\nreader done reading\n";
   wait ();   
   return 0 ;
}

void wait ()
{
cout << "\n\nJust type in anything and press 'enter' to continue\n\n";
char t;
cin >> t;
}
I'm a 15 year old kid who has his heart set on Programming, almost as much as Marss++.......

Came here hoping to ask more questions so if you have any free time just say!
Doctor Salt
 
Posts: 147
Joined: Wed Jul 30, 2008 3:32 pm

Postby C++ on Fri Aug 22, 2008 6:33 pm

Nicely done. It's a good thing to do your own research and learn things as you go by trial and error.

For saving games this isn't a widely used method though. Mostly game state data is saved in binary mode and often encrypted to prevent a user from easily tampering with the game state data. But it's certainly good for starters.
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Postby Doctor Salt on Fri Aug 22, 2008 7:04 pm

Well, this is just my version one of this. By simply replacing the file mode from..

Code: Select all
ofstream writer( "SlotOne.txt", ios::trunc ) ;


to

Code: Select all
ofstream writer( "SlotOne.txt", ios::binary) ;


should do the trick :P
I'm a 15 year old kid who has his heart set on Programming, almost as much as Marss++.......

Came here hoping to ask more questions so if you have any free time just say!
Doctor Salt
 
Posts: 147
Joined: Wed Jul 30, 2008 3:32 pm

Postby Doctor Salt on Fri Aug 22, 2008 7:17 pm

Okay! Heres my vastly improved saving and loading system!

http://rapidshare.com/files/139370924/f ... 1.exe.html

just put it somewhere, and it does the rest. There is two different saving slots, and you can choose which one to "save" to. As of right now, all saving does it remember three lines of text, which is good enough for now. Then you can load either of the two slots, and see what is saved.


If you try to open a file that is blank, the appropriate error message i made comes up, then brings you back again.

As far as i know, the program is totally idiot proof!!
I'm a 15 year old kid who has his heart set on Programming, almost as much as Marss++.......

Came here hoping to ask more questions so if you have any free time just say!
Doctor Salt
 
Posts: 147
Joined: Wed Jul 30, 2008 3:32 pm

Postby C++ on Fri Aug 22, 2008 7:26 pm

Doctor Salt wrote:Well, this is just my version one of this. By simply replacing the file mode from..

Code: Select all
ofstream writer( "SlotOne.txt", ios::trunc ) ;


to

Code: Select all
ofstream writer( "SlotOne.txt", ios::binary) ;


should do the trick :P



The game state is still just string data. A game state normally includes different types of data that describes the state of the game that's saved. That data is represented as hexadecimal in computer memory and on disk. In binary mode it's common to use buffers to process data. Binary data can be any data type or object etc, so type casting is used often. It's more complex than simply reading/writing text files.
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Postby Marss++ on Sat Aug 23, 2008 10:20 am

Ahh... I think Gooby would be the one to talk to about this stuff. He's a profesional game devoloper.



But, I couldn't download your application Salt because my computers a peice of... Anyways, I understand filestreams and I think it's great that people are getting to know what they are, otherwise every time you were to start an application it would be starting all over. This is not the way things should be. So good job, and keep reading up on it.
I'm a 15 year old kid who has his heart set on Programming.

Came here hoping to help so if you have any questions just ask!
Marss++
 
Posts: 149
Joined: Fri Jul 18, 2008 3:20 pm

Postby Doctor Salt on Sat Aug 23, 2008 11:30 am

Oh.. well, if you still want though, heres the code.

Code: Select all
#include "stdafx.h"
#include <string>
#include <conio.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <windows.h>
using namespace std ;

void slotonesave ();
void slottwosave();

void slotoneload ();
void slottwoload ();

void wait ();
void sleep( unsigned int uSeconds );

int main()
{
cout << "Do you want to save to slot one, or two?\n\n";
cout << "Type '1' to save to slot one\n";
cout << "Type '2' to save to slot two \n";
cout << "Type '3' to load one of those slots instead\n\n";

   int choice;
   cin>> choice;

switch (choice)
   {
   case 1:{slotonesave ();}
      break;
   case 2:{slottwosave ();}
      break;
   case 3:{break;}
   default:{main ();}
   }

cout << "Which slot do you want to load now?\n\n";
cout << "Type '1' to load to slot one \n";
cout << "Type '2' to load to slot two \n";
cout << "Type '3' to save data to one of those slots instead\n\n";
   int choice2;
   cin >> choice2;
   

switch (choice2)
   {
   case 1:{slotoneload ();}
      main ();
   case 2:{slottwoload ();}
      main ();
   case 3:{main ();}
   default:{main ();}
   }   
         return 0 ;
}


void slotonesave ()
{
   string a;
   string b;
   string c;
cout << "\nYou have chosen to write to: Slot One";
   Sleep(1500);

cout << "\nEnter your first line of data now\n";
getline( cin, a);
getline( cin, a);
   a.append ("\n");

cout << "\nEnter your second line of data now\n";
getline( cin, b);
   b.append ("\n");

cout << "\nEnter your third line of data now\n";
getline( cin, c);
   c.append ("\n");

   Sleep(1000);
   string slotone = a;
      slotone.append( b ) ;
      slotone.append( c ) ;
cout << "Writing complete!";

   ofstream writer( "SlotOne.txt", ios::trunc ) ;
   writer << slotone << endl;

   Sleep(2000);

   if( ! writer )   
   {
      cout << "Error saving the file" << endl ;
      Sleep(2000);
      main ();
   }
      writer.close() ;
      wait ();

return;
}
void slottwosave ()
{
   string a;
   string b;
   string c;
cout << "\nYou have chosen to write to:Slot two";
Sleep(1500);

cout << "\nEnter your first line of data now\n";
getline( cin, a);
getline( cin, a);
   a.append ("\n");

cout << "\nEnter your second line of data now\n";
getline( cin, b);
   b.append ("\n");

cout << "\nEnter your third line of data now\n";
getline( cin, c);
   c.append ("\n");

   Sleep(1000);
   string slotone = a;
      slotone.append( b ) ;
      slotone.append( c ) ;

cout << "Writing complete!";

   ofstream writer( "SlotTwo.txt", ios::trunc ) ;
   writer << slotone << endl;

   Sleep(2000);

   if( ! writer )   
   {
      cout << "Error saving the file" << endl ;
      Sleep(2000);
      main ();
   }
      writer.close() ;

return;
}

void slotoneload ()
{
   int i;
   string line ;

   cout << "\nNow loading Slot One...\n";
   Sleep(2000);
   ifstream reader( "slotOne.txt", ios::in ) ; 

   if( ! reader )
   {
      cout << "Error loading file" << endl ;
      cout << "There most likely isn't anything in the folder\n" << endl;
      Sleep(2000);
      main (); ;
   }
   else
   for( i = 0; ! reader.eof() ; i++ )
   {
      getline( reader , line ) ;
      cout << line << endl ;
   }

   reader.close() ;
   
   //cout << "Iterations: " << (i/2) << endl ;
   cout << "Loading Complete\n";
   wait ();
   return;
}
void slottwoload ()
{
   int i;
   string line ;

   cout << "\nNow loading Slot Two...\n";
   Sleep(2000);
   ifstream reader( "slotTwo.txt", ios::in ) ; 

   if( ! reader )
   {
      cout << "Error loading file" << endl ;
      cout << "There most likely isn't anything in the folder\n" << endl;
      Sleep(2000);
      main (); ;
   }
   else
   for( i = 0; ! reader.eof() ; i++ )
   {
      getline( reader , line ) ;
      cout << line << endl ;
   }

   reader.close() ;
   
   //cout << "Iterations: " << (i/2) << endl ;
   cout << "Loading Complete";
   wait ();
   return;
}

void wait ()
{
cout << "\nPress any key to continue\n";
while(!kbhit()) ;
return;
}
I'm a 15 year old kid who has his heart set on Programming, almost as much as Marss++.......

Came here hoping to ask more questions so if you have any free time just say!
Doctor Salt
 
Posts: 147
Joined: Wed Jul 30, 2008 3:32 pm

Postby Marss++ on Sat Aug 23, 2008 5:51 pm

Looks Good!

If I may...


One thing to the actual layout of the code.
I would change this bit of code:

Code: Select all
cout << "Writing complete!";

   ofstream writer( "SlotTwo.txt", ios::trunc ) ;
   writer << slotone << endl;

   Sleep(2000);

   if( ! writer )   
   {
      cout << "Error saving the file" << endl ;
      Sleep(2000);
      main ();
   }
      writer.close() ;

return;
}



to this:

Code: Select all
ofstream writer( "SlotTwo.txt", ios::trunc ) ;
   writer << slotone << endl;

   Sleep(2000);

   if( ! writer )   
   {
      cout << "Error saving the file" << endl ;
      Sleep(2000);
      main ();
   }
   else
   {
      cout << "Writing complete!";
   }
      writer.close() ;

return;
}



This is because regardless of whether or not the file opened,
it will say "Writing Complete". The new version will make it say this only
if it succesfully saved.

This is not a huge mistake at all, but you could imagine it would look kind of funny for the screen to say "Writing Complete" and then contridict it in the next line.

Other then that, Your code Looks really really good, and you did a good job of using the string class.

Great Job Salt!
I'm a 15 year old kid who has his heart set on Programming.

Came here hoping to help so if you have any questions just ask!
Marss++
 
Posts: 149
Joined: Fri Jul 18, 2008 3:20 pm

Postby Doctor Salt on Sat Aug 23, 2008 9:54 pm

Ah, i find that really funny. Must of happened when I was rearranging tons of my program. I guess i didn't notice that.....
I'm a 15 year old kid who has his heart set on Programming, almost as much as Marss++.......

Came here hoping to ask more questions so if you have any free time just say!
Doctor Salt
 
Posts: 147
Joined: Wed Jul 30, 2008 3:32 pm


Return to -- Code Practice Submissions --

Who is online

Users browsing this forum: No registered users and 0 guests