how to save on a file

i want to know how can i save an object of a class on a file
this doesnt work:
(project is a class and database is an object of it )

void save(project dataBase){
ofstream out("sam.txt");
out.write((char*)&dataBase,sizeof(dataBase));
}
I think you need to create memberfunctions for that, eg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class data
{
  public:
    void load(string filename);
    void save(string filename);
  private:
    string myData;
}

void data::load(string filename)
{
    ifstream in;
    in.open(filename);
    in.getline(myData);
}

void data::save(string filename)
{
    ofstream out;
    out.open(filename);
    out<<myData;
}
doesnt it make any difference wether myData is string or a vector<vector<vector <string>>>?(i mean a 3dimentional vector!)
You have to figure out some sort of system, so your program can tell how many elements it has to store in each dimension. For example, the .txt file where you save the data could look like this:

5 9 3
1901
1920
1918
3021
3078
3034
...

Here, we store threedimensional array into a file. The size of the array is not know, so first, we print the number of elements. In this case, the array is [5][9][3]. Then we can allocate that memory/declare the nessesary vectors and find some way to store the data in them.
The datafile can look complelty different: it's just how you designe it.
i just didnt understand!
i have a class named project in which there is a data of type vector<vector<vector<string>>> and i have chosen type vector because i dont know anything about the size. and the data in this type is going to be a set of tables, and i want to save this set of tables! what must i do now?
Can you plz post the class defenition?
in fact there are 2 classes, az u see:
(sry for the bad font . i cant have them the way u did!)

//begining of table class

class table{

public:
vector< vector<string> > v;

int number;

//CREATE BEGIN
table (){}

~table (){}

table create(){

string b;

vector<string> v1;
cout << "plz enter the umber of the table";
cin >> number;
cin >>b;


while(b!= "end"){
v1.push_back(b);

cin>>b;

}

v.push_back(v1);

table t;

t.v = v;


return t;
}

//CREATE END



//INSERT BEGIN

void insert(){
vector<string> v1;


for (int i=0;i< v[0].size();i++){
string b;


cin>>b;

v1.push_back(b);

}

v.push_back(v1);


}

//INSERT END
};

//end of table class


//begining of project class

class project{
public:
vector<table> db;




//begining of readinput function

void readinput(string str, project dataBase){

int number;
table t;


if (str=="create"){
cout<<dataBase.db.size()+1 <<endl;

dataBase.db.push_back(t.create());

}


if(str=="show"){
cout<<"Please enter the number of table\n";
cin>>number;


//t.push_back(db[number-1]);

//t=db[number-1];
cout <<"sizse is " << t.v.size();
dataBase.show(t);

}


if(str=="insert"){
cout<<"Please enter the number of table\n";
cin>>number;

db[number-1].insert();

}

}

//end of readinput function
void show(table t){
vector<vector<string> > vn;

vn=t.v;


for (long int i=0;i< vn.size() ;i++){

for ( int j=0; j< vn[0].size() ;j++){
cout<< vn[i][j];


/* int l=sizeof(vn[0][j]);
int y;

int z;

z=sizeof(vn[i][j]);

if (l<10)

y=10-z;

else y=2;

for(int f=1;f<=y;f++){

cout<<" ";

}*/


}

cout<<endl;

}

}


//void link/////
};
You can use special font for code with the code-tag (#format on the right when posting).

Oke, so you got a unknown number of tables, and each table has an unknown x and y size. On the first line, I would print the number of tables. Then the x and y size from the first table. Then the first table. Then the x and y size from the second table, etc. etc.
You can create your own structure in the file, as long as you keep to that structure when writing to and reading from the file.
Topic archived. No new replies allowed.