Public Group

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

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

## 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 on other sites
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 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

Ah, thanks!

##### 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( );

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

##### Share on other sites
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 fileofstream ofile("save.bin")if (ofile.isOpen()){   ofile << player.hp << " " << player.level;   ofile.Close();}

You would need

ifstream ifile("save.bin") // Open the file for readingif (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 on other sites
Yes, it's written as you said. And yes i use Cpp. And thanks for the info!

##### 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 on other sites
That cplusplus.com is a poor place to learn standard C++ from.

// ville

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634138
• Total Posts
3015758
×