Jump to content
  • Advertisement
Sign in to follow this  
Samsonite

How do i load data from a file and use it as a variable value?

This topic is 4839 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

Lets say i write to save.bin all the data that the player has(HP, LVL, whatever). Now how do i load those values back in into the game(HP values at HP, LVL values at LVL, etc)? Somebody told me that i needed to create some operator in my class called >. But i forgot, it's a long time ago. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Yes, yes!I have! Thats my primary source for C++ learning, but it only covers getting whole files into a buffer and not controlling what lines to get into wich buffer(using cin.getline(buffer,number))

EDIT:Oh! I might wan't to use seekg(), seekp(), tellg() and tellp() functions right?

Share this post


Link to post
Share on other sites
The section near the bottom on binary files? It's a simple matter of changing the size in the call to read(). Note that binary files don't necessarily have lines separating the data (that's why when you open one in a text editor you get unwrapped gobbledygook), so the first few examples on that page are indeed for text files only.

And yes, you can use the seek functions to move back in forth in the file, although it's generally a good idea just to start at the beginning and read everything into your memory right away.

Although the functions given specify char * as the data to read and write, you can also write other data types as long as you remain aware of their sizes (the sizeof() operator is handy here).

Cheers,
Twilight Dragon

Share this post


Link to post
Share on other sites
Since all your data is related, it would probably be easier to put it in one structure and read/write it all at once like so:

struct player_data
{
char name[20];
int hp;
int level;
};

player_data data;

Then you can write it all at once with:

std::fstream file( "save.bin", std::ios::out | std::ios::binary );
file.write( (char*)&data, sizeof(player_data) );
file.close( );

And read it back with:

std::fstream file( "save.bin", std::ios::in | std::ios::binary );
file.read( (char*)&data, sizeof(player_data) );
file.close( );

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Well, it depends on how you wrote to your save.bin file. Is it in a binary format or plain text? Are there other things saved in this file other than just player data? What language are you using?


Anyway you look at it, you need to give us more information than what you gave us. All that i can tell you with what was given is that if you write to your save.bin file you will need to read the data in the same order as you saved it unless you want to jump around the file, which is silly.


If it was in CPP, you could have something like this... Note it is untested snippet of code.

// Save the data to the file
ofstream ofile("save.bin")
if (ofile.isOpen()){
ofile << player.hp << " " << player.level;
ofile.Close();
}



You would need


ifstream ifile("save.bin") // Open the file for reading
if (ofile.isOpen()){
ifile >> player.hp >> player.level;
// if you need to read in a string that could possibly have spaces, tabs,
// etc you will need to use ifile's getline or get method to read in that string until it comes across a delimiter or the number of bytes have been read in.
ifile.Close();

}



Share this post


Link to post
Share on other sites
Read data in a parallel way to how you write it. An operator>> for your class is often a good idea, especially if you're also writing via an operator<<. You might also consider writing a constructor which takes the stream as an argument (by non-const reference), and initializes members with read data. That avoids the need to construct a 'temporarily garbage' object to read into, but won't give you the benefit of operator chaining.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
That cplusplus.com is a poor place to learn standard C++ from.



// ville

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!