Sign in to follow this  

Saving Information

This topic is 4640 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering, how do you save information in a game. Like in this stupid text based game I have(all my games are like that), it requires a username and password. I was wondering, how do you save the password and username? And once i do, how do I let the program know what usernames go with what passwords? Thanks

Share this post


Link to post
Share on other sites
Well, how I would do it is; have a function that saves and calls the file I/O for the password and username.

void saveGame();
//...
void saveGame()
{
//file I/O here...
}
//...

if (blah)
{
saveGame();
}
//...



EDIT:Too slow.

Share this post


Link to post
Share on other sites
To keep track of what usernames go with what passwords, just put them next to each other.

Write username
Write password
Write username
Write password
etc...

Then, when someone enters a username and password, loop through all the information in the text file until either you find a username and password that match what was entered, or you don't find a combo that match and you thus tell the player they did not enter a correct username or password.

If you are confused on file IO, here is an excellent tutorial:

http://www.gamedev.net/reference/articles/article1127.asp

P.S. THis will not be secure at all. Someone could simply open the text file to read the passwords. My suggestion is to change the extension from .txt to .pwd or something. That will at least deter the casual player.

Share this post


Link to post
Share on other sites
Actually, Expat is a better XML library, but it only does reading, not writing. Of course, who needs writing really, that part is easy. Anyways, lets consider some code. I am of the belief however that "PASSWORDS" are properly encrypted as the point of input and hashes are compared, as opposed to back and forth encryption and decryption.
typedef struct UserFiles
{
char username[256];
char password[256];
} UserFiles_T;

UserFiles_T users[256];

int save_passwords()
{
FILE * outfile;
int x;
outfile = fopen( "userrecords.dat", "w" );
if (!outfile) return 0;

fprintf( outfile, "<?xml version="1.0"?>\n<userarchive>\n" );
for ( x=0; x<256; x++ )
{
fprintf( outfile, " <user name=\"%s\" password=\"%s\"/>\n",
users[x].username, users[x].password );
}
fprintf( outfile, "</userarchive>\n" );
fclose( outfile );
return 1;
}


ASSUMING that you're using an XML system like expat, then this would work great.

Share this post


Link to post
Share on other sites
Quote:
Original post by Inmate2993
Actually, Expat is a better XML library, but it only does reading, not writing. Of course, who needs writing really, that part is easy.
How is it better if it lacks features like writing? I've never tried out Expat, but I have used TinyXML quite a bit and must say that it's very powerful.

Share this post


Link to post
Share on other sites
If you are just doing something this simple, you do not need to go and use XML really. If you are just using this for a name password system, STL is your best friend. This took me less than 30 mins:


#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <vector>

// http://www.gamedev.net/community/forums/viewreply.asp?ID=1924249
std::vector<std::string> tokenize(const std::string & str, const std::string & delim);

using namespace std;

map<string,string> UserList;

// Case sensitive!
void AddUser( string username, string password )
{
// Just add it to the map
UserList[ username ] = password;
}

// Case sensitive!
void RemoveUser( string username )
{
// Just clear the password
UserList[ username ] = "";
}

void Save( string filename, char key )
{
stringstream str;
for( map<string,string>::iterator itr = UserList.begin(); itr != UserList.end(); itr++)
{
if( itr->second != "" )
str << itr->first << "~" << itr->second << endl;
}

string temp = str.str();
for( int x=0;x<temp.size();x++)
{
temp[x] ^= key;
}

ofstream OF;
OF.open( filename.c_str() );
OF << temp;
OF.close();
}

void Load( string filename, char key )
{
ifstream IF;
IF.open( filename.c_str() );

string str = "";
char buffer[1024];
memset( buffer, 0, 1024 );
while( IF.read(buffer,1024) )
{
str += buffer;
memset( buffer, 0, 1024 );
}
str += buffer;

string temp = str;
for( int x=0;x<temp.size();x++)
{
// Simple yet effective XOR encrption
temp[x] ^= key;
}

IF.close();

// Time to parse!
vector<string> tokens = tokenize(temp,"~\n");
for( x=0; x < tokens.size(); x+=2 )
{
AddUser( tokens[x], tokens[x+1] );
}
}

void DisplayAll()
{
for( map<string,string>::iterator itr = UserList.begin(); itr != UserList.end(); itr++)
{
if( itr->second != "" )
cout << itr->first << " " << itr->second << endl;
}
}

int main( int argc, char** argv)
{
// Add the users
AddUser("Drew Benton","123");
AddUser("Rob Loach","456");
AddUser("Inmate2993","789");

// Save with the key
Save( "passwords.txt", 'A' );

// Show it all
DisplayAll();

cout << "Clearing the list!" << endl << endl;

// Now clear it
UserList.clear();

// Show that we did clear it!
DisplayAll();

// Now load it in
Load( "passwords.txt", 'A' );

// Show it all loaded!
DisplayAll();

return 0;
};

std::vector<std::string> tokenize(const std::string & str, const std::string & delim)
{
using namespace std;
vector<string> tokens;

size_t p0 = 0, p1 = string::npos;
while(p0 != string::npos)
{
p1 = str.find_first_of(delim, p0);
if(p1 != p0)
{
string token = str.substr(p0, p1 - p0);
tokens.push_back(token);
}
p0 = str.find_first_not_of(delim, p1);
}
return tokens;
}



Have a look to see what concepts are being used. If you have never seenthis much STL use before (map,iterator,vector), then I strongly suggest learning it. Thanks to Fruny for getting me intrested in standardized C++. I would have never tried STL otherwise. Hopefully you should be able to follow along, if not, feel free to ask. You can use this as a model to get what you need to do - I do not think it is 'everything' you wanted, but if it is less work for you [smile].

Anyways, this example uses the most basic XOR encryption, but anyone just looking at the file will never be able to figure it out unless they knew you used XOR encrpytion. You could definitly use a strong protection if you really wanted to as well. Take a look at TEA. Compliments to Mr. Loach for that question about it and smart_idiot for his 'free time' [lol].

I think that's about it now. Can't think of anything else really. It's written in my own "C+" style, mixture of C and C++, so that's why I have a memset in there as well as no classes. I'm sure you could do a bit OOP'ing to it if you wanted to integrate it into your projects.

- Drew

Share this post


Link to post
Share on other sites
Quote:
Original post by Rob Loach
Quote:
Original post by Inmate2993
Actually, Expat is a better XML library, but it only does reading, not writing. Of course, who needs writing really, that part is easy.
How is it better if it lacks features like writing? I've never tried out Expat, but I have used TinyXML quite a bit and must say that it's very powerful.

Expat has only a 100k footprint, as opposed to the 900k footprint I've seen other xml libraries have.

Plus, expat works in callback functions and buffers, so you can start parsing midway, rather then having to load an entire file.

Share this post


Link to post
Share on other sites
Quote:
Original post by Inmate2993
Quote:
Original post by Rob Loach
Quote:
Original post by Inmate2993
Actually, Expat is a better XML library, but it only does reading, not writing. Of course, who needs writing really, that part is easy.
How is it better if it lacks features like writing? I've never tried out Expat, but I have used TinyXML quite a bit and must say that it's very powerful.

Expat has only a 100k footprint, as opposed to the 900k footprint I've seen other xml libraries have.

Plus, expat works in callback functions and buffers, so you can start parsing midway, rather then having to load an entire file.
Point taken, good sir. I'll have to try it out one day.

Share this post


Link to post
Share on other sites

This topic is 4640 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this