# Thread: Text-based game, saving progress into some file, .sav? How to?

Code:
```            if(ctemp == '0')
temp = 0;
if(ctemp == '1')
temp = 1;
if(ctemp == '2')
temp = 2;
if(ctemp == '3')
temp = 3;
if(ctemp == '4')
temp = 4;
if(ctemp == '5')
temp = 5;
if(ctemp == '6')
temp = 6;
if(ctemp == '7')
temp = 7;
if(ctemp == '8')
temp = 8;
if(ctemp == '9')
temp = 9;```
You could've just use
Code:
`temp = ctemp - '0';`
I think you overcomplicated the loading process a bit, you could've just use loops like you did in saving process, in which you would extract one char from the stream at a time (char c; stream >> c; ), and since you know, you always get non-whitespace one, and you don't write anything other than digits and whitespaces, you can just put the c-'0' to your GameBoard.

2. ahh thats clever didn't thnk about that. so if the number was two by subtracting the default numberical value of char '0' I would get 2 left over to save as an int... thnx. A lil more outside the box than I was thinking.

3. Originally Posted by phantomotap
O_o

The situation is exactly the same in all languages.

You will need to know how to handle any data stream not managed by an available library if you want to work with that data.

If a library that manages a data stream is available, you can safely ignore the details.

If a library that manages a data stream is not available, you must learn the details.

And you have missed the obvious case of creating your own data stream.

Soma
Of course you would have to know if you're dealing with low level stuff. But how often are you dealing with low level stuff?
Compute integral from 0 to infinity of dealing with low-level stuff with respect to time, divided by the number of people using the language. Then compare that number for C and C++.
The number for C++ should be radically lower. Thus it's not as important average.

Originally Posted by Lesshardtofind
Wow your solution is soo different than the one I've been using. I just saved data as strings and then converted the ones I wanted to be ints to ints. Is my method bad?

Stores a current sudoku and its solution then loads a current and its solution. Converts the strings to its individual char and then into a int. I just read about fstreams and this was the solution I came up with. I'm sure its flawed if you feel like pointing out why I'd appreciate it.
It's not bad. It just involves a lot of more work. Plus the abstraction level is lower, so if you make a change, chances are you'll break a lot of code.

4. Thanks for help, everyone! I've made a little program, just to check, if it works:

Code:
```#include <fstream>
#include <iostream>
using namespace std;
// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

/////////////////////////////////////////////////////////////
// gps coordinate
//
// illustrates serialization for a simple type
//
class gps_position
{
private:
friend class boost::serialization::access;
// When the class Archive corresponds to an output archive, the
// & operator is defined similar to <<.  Likewise, when the class Archive
// is a type of input archive the & operator is defined similar to >>.
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & degrees;
ar & minutes;
ar & seconds;
}
int degrees;
int minutes;
float seconds;
public:
gps_position(){};
gps_position(int d, int m, float s) :
degrees(d), minutes(m), seconds(s)
{}
};

int main() {
// create and open a character archive for output
std::ofstream ofs("filename.txt");

// create class instance
const gps_position g(35, 59, 24.567f);

// save data to archive
{
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << g;
// archive and stream closed when destructors are called
}

// ... some time later restore the class instance to its orginal state
gps_position newg;
{
// create and open an archive for input
std::ifstream ifs("filename.txt");
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> newg;
// archive and stream closed when destructors are called
}
cin.get();
return 0;
}```
Actually copied the code from the website, and edited it a little bit. I've tried cout'ing the variable too, but it didn't worked for some reason. The important thing is, that i get around 70-80 percent on how it works, after Elysia (thanks!) explained it in one of the posts how it works. 70-80 percent, because I do not understand, what is not working correctly now.

It outputs this to the .txt file: 22 serialization::archive 7 0 0 35 59 24.566999
Also cout is not working for some reason...

5. You mean
cout << newg?
That wouldn't work because newg does not have any overloaded << operator.

6. Hmmm. Okay, I don't get how it works again, lol.

I've tried this thing:
Code:
```#include <fstream>
#include <iostream>
#include <string>
using namespace std;
// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

/////////////////////////////////////////////////////////////
// gps coordinate
//
// illustrates serialization for a simple type
//
class gps_position
{
private:
friend class boost::serialization::access;
// When the class Archive corresponds to an output archive, the
// & operator is defined similar to <<.  Likewise, when the class Archive
// is a type of input archive the & operator is defined similar to >>.
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & degrees;
ar & minutes;
ar & seconds;
}
int degrees;
int minutes;
float seconds;
public:
gps_position(){};
gps_position(int d, int m, float s) :
degrees(d), minutes(m), seconds(s)
{}
};

int main() {
string d;
// create and open a character archive for output
std::ofstream ofs("filename.txt");
int dd;
//// create class instance
//const gps_position g(35, 59, 24.567f);

//// save data to archive
//{
//    boost::archive::text_oarchive oa(ofs);
//    // write class instance to archive
//    oa << g;
//	// archive and stream closed when destructors are called
//}
boost::archive::text_oarchive one(ofs);
int var = 231;
one << var;
std::ifstream ifs("filename.txt");
boost::archive::text_iarchive two(ifs);
two >> dd;
// ... some time later restore the class instance to its orginal state
//gps_position newg;
//{
//    // create and open an archive for input
//    std::ifstream ifs("filename.txt");
//    boost::archive::text_iarchive ia(ifs);
//    // read class state from archive
//    ia >> d;
//    // archive and stream closed when destructors are called
//}
cout << dd;
cin.get();
return 0;
}```

7. filename.txt is probably still open by ofs; thus ifs fails to open it and thus boost throws an error.

8. Hmmm. That's what i thought actually, until the memory error came up. Wasn't sure, what exactly that meant. Anyway, I've read the tutorials and reference given in the Serialization website. A quick read actually. But i was unable to find some one-line command, to close the file. It isn't possible using Serialization, is it? You must write classes, like it was done in the tutorial, right? But how to cout something then? You cannot cout the class or function... The thing, that is in main() loop and looks like a function, not sure, how to call that exactly, haven't met such before.

9. To close a file explicitly, call close() on the file object.
You can serialize anything you want, from mere ints to user-defined objects to vectors!
You can cout stuff, but this is unrelated to boost. You must overload operator << with a left-hand argument of ostream and right-hand argument of whatever yo want to print.
You can't print a function, obviously.

10. Hmmm. Okay, I probably should know more about classes first, but I'm going to ask this anyway (classes was my second thing to learn deeper, after the save/load thing.):

Do i have to work with this class, and do everything somehow in class? Can't I make variables somehow to change outside the class, using a function instead?

11. C++ isn't Java; you are not limited to OOP.
You can create and modify variables outside classes. You can also create functions outside classes.

12. Hmmm. Okay. But how am I supposed to use that neccesary class from Serialization with outside functions then? I don't even know, what this means:

Code:
```    gps_position(){};
gps_position(int d, int m, float s) :
degrees(d), minutes(m), seconds(s)
{}```
It is not a function, functions doesn't have : in the end of the declaration, and functions do have variable type in the beginning. So I don't have an idea, what it is. It has no declarations, whatsoever.

13. They are constructors. The ":" is an initializer list.
They are not necessary for Serialization to work.