Save instances of a class in a file?

Hello.

Q1: I want to create a creature "dog" with id=1, and a creature "cat" with id=2, using the following class
How can I store the instances in a file "creature.dat" and how can I read the stored information from the file?
If I include private data member in the class, will that change anything?


1
2
3
4
5
6
7
8
9
10
11
12
class Creature
{
public:
    int m_Name;  
    int m_id;
    void Greet(); 
};

void Critter::Greet()    // member function definition
{
    cout << "Hi. I'm a creature. My name is " << m_Name << ".\n";
}



Q2: The above example is a simple one because right now I just want to know how the "save" part works. The goal is to create a wrestling simulator.
From what I've heard vectors are used when the type of the values are of the same type (int,char,..). However, for the wrestlers I will need different types since they will have name, level, experience points,... Is a class the best way to group all the information that define a wrestler, or do you propose some other way?

PS: Sorry for my English and thanks.









Last edited on
Ans 1: To write a instances ina a file you just to:

string fich="nameFile.dat"
fstream f;
Creature creat;

f.open(fich,ios::in | ios::out);
f.write(&creat,sizeof(Creature));
f.read(&creat,sizeof(Creature));
As for Q2, yes I would use classes for this task. Vectors and such are great for lists of data but it would be tricky to access the individuals like you would want to with that method.
Thank you both for the quick responses.
As I said, I want to write more than one instances in the file. Is it be possible to retrieve one of them without having to access the other?

Could someone show me an example with 2 instances?
Last edited on
Yeah it's possible, it's not how I would do it because you'll end up with a data file for everyone of your characters but here's something like what you would do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

//...Code Code Code
ifstream Player_One("PlayerOne.dat", ios_base::in);
ifstream Player_Two("PlayerTwo.dat", ios_base::in);
//...etc.
Creature P1, P2, //...etc;

while(Player_One)
{Player_One>>P1.Name;
 Player_One>>P1.Id;
//...etc.}

//...Repeat As Needed

//...Code Code Code

Player_One.close();
Player_Two.close();
//...etc.


This sounds like what you want. Just make sure that the statlines are written into the file in a specific order, which if you write a program to do that it shouldn't be that hard. Also what you have written as the void Greet() should be a char array or string that you also read from the file.

EDIT: If you mean to get it all from the same file then you would have to use some logic for obtaining the data something like this:

Data File:
1
2
3
Name Jon
Size Big
//...etc 


Program Code:
1
2
3
4
5
6
7
//...Code Code Code
Player_One>>strCheck; //<- String variable for holding what is in Player_One
if(strCheck == "Name")
{Player_One>>P1.Name;} /*<- Line following "Name" is known ahead of time to be the Name*/

//...Etc Rinse and Repeat

Last edited on
Topic archived. No new replies allowed.