I have no idea how to make somthing like this, and i'm kind of new with file I/O.
How would you start somthing like this?

O.k. here is what I have to save,
I have roughly 56+ variables that need to be counted in this.
But what I am having trouble with is when you write in a word doc it can't save the actual vairable values.
I need to save like if somthing is 1 or 0. Like if you equiped a weapon or not, or if a quest if active. And then acess that when you load the game back up, but I can't seem to get the variable to show up when you access the load screen so the game locks up when you access portions of the map that "are not acessed yet." Which should have been saved during the previous play.
(it's also annoying when you have to start over agian when you start up the game)
For example,
int x;
x = x + 1;
I need to save the value of x, being that it is a coordinate to the map, and load it from the file agian exactly as it saved it.

Anybody know how to do this, or at least give me a brief walkthrough with file I/O and or how to access and save to files?

Here is an example of two code snippets;

savefile1.open ("savefile1.txt", ios::out | ios::app);
goto maptest;
savefile1.open ("savefile1.txt");
savefile1 <<"x ="<<x<<"\n"<<"y ="<<y<<"\n";
goto jumpto;

could someone tell me how to access those x and y variables?

Yes this was orginally posted in game dev, but i would think i would have more success with an answer here.

Recommended Answers

All 6 Replies

you will need to do some form of string parsing. Firstly you need to read from the file you can use the following method

ifstream f("savefile1.txt", ios::in, ios::binary);
string line;

getline(f, line);

once you have something to work with then you can begin parsing the string.
string streams useful here,

#include <sstream>
ostringstream streams;
string words[3];
streams >> words[1] >> words[2] >> words[3];

That way word[1] would be "y" word[2] would be "=" and word[3] the variable.

Another method would be to read the file word by word using the >> operator.


Sorry this did not help. Your thing with >> doesn't work at all. Could you please explain how and what it does exactly so I can at least understand how it works, and figure out how to write it to fit my code.

Or could someone tell me how to load up a file, and change the variables in a program to the numbers of the file, scince that's exactly what I am trying to do.

Ok, please help me.
Here is another recap of what i need to do.

I need to read off a file a number.

I need to change a variable to that number that is stored on the file.

i Should note it should be 0..1..2 on my array in the post very accidental mistake sorry


You should try to make the question as limited as possible.

"I would like to write a number to a file, and then later retrieve that number from a file." is much clearer than something about a game and a quest, etc.

Try to get a sample application working (using unbeatables post as a starting point) before you try to integrate it into your game. You sample program should do exactly what you're trying to do and nothing else (ie. write a number to a file, and then read it in again.)

If you get that to work on its own, you should have no problem integrating it into the game.

Post back with results from the sample program.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.