First attempt Load/save program

Yesterday I was looking through tutorials, and nothing really made much sense to me. The first parts made sense, but then it seemed like a huge technological jump took place in the writing. So, I took a stab in the dark yesterday - and it doesn't work as I want it to. It compiles with no errors, just doesn't give the correct numbers... at all.

I left out the int main and #include statements.
So here's the basic script:
1
2
3
4
5
6
7
8
9
10
int wood = 5;
ofstream fout("save.txt");
ifstream fin("save.txt");
fin >> wood;
cout << "Your current wood: " << wood;
wood = 6; //NOTE! This actually changes in the game. Just makes it easier to see if it saves.
fout << wood;
fout << flush;
fout.close();
system("Pause");
Last edited on
Why you open SAME file in different streams before closing even 1?

For example:
First open file for reading data.
Read the data from the file.
Close the file.

Now it would be time to modify data...

Now open a file (=stream) again but this time for "printing" data to the file.
Insert the data to the file.
Close the output file
Why you open SAME file in different streams before closing even 1

I have no idea...

So should it be done like this?

1
2
3
4
5
6
7
8
9
10
11
int wood = 5;
ifstream fin("save.txt");
fin >> wood;
fin.close();
cout << "Your current wood: " << wood;
wood = 6; //NOTE! This actually changes in the game. Just makes it easier to see if it saves.
ofstream fout("save.txt");
fout << wood;
fout << flush;
fout.close();
system("Pause");


EDIT: I tested it just now, and it seems like it's working great! Thanks for the help.
Last edited on
Topic archived. No new replies allowed.