How can i save vectors of type class in file and read it

I am currently using below program to write one object of class student inside a file and than read it but as i try to save more than one object than read it the output is not correct:
To read an object:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <fstream>
#include <string.h>
#include <iostream>
#include <vector>
using namespace std;
class Student {
public: int a,b;
};

int main() {
	vector<Student> one;
        Student s;
        s.a=2; s.b=3;
        one.push_back(s);

        s.a=23; s.b=11;
        one.push_back(s);
	ofstream ofs("fifthgrade.ros", ios::binary);
        ofs.write((char *)&s, sizeof(s));
        for(int i=0;i<one.size();i++) {       
           cout<<one[i].a<<"\t"<<one[i].b<<endl; 
        }
	
	return 0;
}

Here i have created two vectors which contains two objects. Any other way in which i can save more than one object inside a file is ok for me. To read a file :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <fstream>
#include <string.h>
#include <iostream>
#include <vector>
using namespace std;
class Student {
public: int a,b;
};

int main() {
	vector<Student> two(1);
	ifstream ifs("fifthgrade.ros", ios::binary);
	ifs.read((char *)&two, sizeof(two));
        cout<<two[0].a<<endl;
	return 0;
}

I get segmentation fault when i do this. Is their any way in which i can save group of objects either of class or struct inside a file and than again read it like above.
Last edited on
misread post.
Last edited on
Maybe like this - store the size, and then the data. In a vector, the elements are stored in consecutive memory locations so this should be ok. There might be safer ways to approach this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <fstream>
#include <iostream>
#include <vector>

using namespace std;

class Student {
public: 
    int a;
    int b;
};

void create_file()
{
    vector<Student> one;
    Student s;
    s.a=2; s.b=3;
    one.push_back(s);
    
    s.a=23; s.b=11;
    one.push_back(s);
    
    ofstream ofs("fifthgrade.ros", ios::binary);
    unsigned size = one.size();
    
    ofs.write( reinterpret_cast<char *>(&size), sizeof(size));
    ofs.write( reinterpret_cast<char *>(one.data()), sizeof(Student) * size );
    
    std::cout << "\nTo file\n";    
    for (size_t i=0; i<one.size(); i++) 
    {       
        cout << one[i].a << "\t" << one[i].b << '\n'; 
    }    
}

void read_file()
{   
    ifstream ifs("fifthgrade.ros", ios::binary);
    
    unsigned size; 
    ifs.read( reinterpret_cast<char *>(&size), sizeof(size));
 
    vector<Student> two(size);
    ifs.read( reinterpret_cast<char *>(two.data()), sizeof(Student) * size );
    
    std::cout << "\nFrom file\n";
    for (size_t i=0; i<two.size(); i++) 
    {       
        cout << two[i].a << "\t" << two[i].b << '\n'; 
    }    
}

int main() 
{
    create_file();

    read_file();
    
}
Topic archived. No new replies allowed.