cant use object serialization to read an object

I'm using write.cpp to store map values inside a fifthgrade.ros file as:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>
#include <string>
#include "map.h"
using namespace std;
    int main() {
        bigarray one;
        one(1,1,1,1,1,1,1,1,1,1,1,1,1)=3;
	ofstream ofs("fifthgrade.ros", ios::binary);

	ofs.write((char *)&one, sizeof(one));
}

I'm using read.cpp file to read the saved file:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>
#include <string>
#include "map.h"
using namespace std;
    int main() {
        bigarray two;
	ifstream ifs("fifthgrade.ros", ios::binary);

	ifs.read((char *)&two, sizeof(two));
        cout<<two(1,1,1,1,1,1,1,1,1,1,1,1,1)<<endl;
}


The header file map.h which contains the class difinition is:

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <map>
#include <iostream>
using namespace std;
struct dim {
	int dim1;
	int dim2;
	int dim3;
	int dim4;
	int dim5; int dim6; int dim7; int dim8; int dim9; int dim10; int dim11; int dim12; int dim13; 

	dim(int d1, int d2, int d3, int d4, int d5, int d6, int d7, int d8, int d9, int d10, int d11, int d12, int d13) : dim1(d1), dim2(d2), dim3(d3), dim4(d4), dim5(d5), dim6(d6), dim7(d7), dim8(d8), dim9(d9), dim10(d10), dim11(d11), dim12(d12), dim13(d13) {}
};

class bigarray
{
public:
	int& operator()(int d1, int d2, int d3, int d4, int d5, int d6, int d7, int d8, int d9, int d10, int d11, int d12, int d13)
	{
		return myarray[dim(d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13)];
	}

	bool exists(int d1, int d2, int d3, int d4, int d5, int d6, int d7, int d8, int d9, int d10, int d11, int d12, int d13) const
	{
		return myarray.count(dim(d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13));
	}

private:
	map<dim, int> myarray;
};

bool operator<(const dim& ld, const dim& rd)
{
	if (ld.dim1 < rd.dim1)
		return true;

	if (ld.dim1 == rd.dim1) {
		if (ld.dim2 < rd.dim2)
			return true;

		if (ld.dim2 == rd.dim2) {
			if (ld.dim3 < rd.dim3)
				return true;

			if (ld.dim3 == rd.dim3) {
				if (ld.dim4 < rd.dim4)
					return true;

				if (ld.dim4 == rd.dim4) {
					if (ld.dim5 < rd.dim5)
						return true;

				if (ld.dim5 == rd.dim5) {
					if (ld.dim6 < rd.dim6)
						return true;

				if (ld.dim6 == rd.dim6) {
					if (ld.dim7 < rd.dim7)
						return true;

				if (ld.dim7 == rd.dim7) {
					if (ld.dim8 < rd.dim8)
						return true;

				if (ld.dim8 == rd.dim8) {
					if (ld.dim9 < rd.dim9)
						return true;

				if (ld.dim9 == rd.dim9) {
					if (ld.dim10 < rd.dim10)
						return true;

				if (ld.dim10 == rd.dim10) {
					if (ld.dim11 < rd.dim11)
						return true;

				if (ld.dim11 == rd.dim11) {
					if (ld.dim12 < rd.dim12)
						return true;

				if (ld.dim12 == rd.dim12) {
					if (ld.dim13 < rd.dim13)
						return true;
                                }}}}}}}}}
			}
		}
	}
	return false;
} 

I am using these code for object recognition task in computer vision. My problem is when i try to read the file it shows segmentation error.
Last edited on
You seem to have a list of values that are all of the same type. Can please use an array to store them. It will make the code easier to read and reason about.
This is how your outputfile looks - no wonder that you can't read it.
08 D0 6A 00 80 CF 6A 00 01 00 00 00

ofs.write((char *)&one, sizeof(one));
You can't just write an object that contains other objects like this.
What is the purpose of your code?
Well i tried to use array at first to store these numbers but since the array has 13 dimensions like arr[1][2]..[13], i cant use that. When i use an object of simple class like :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Student
{
public:
	char   FullName[40];
	char   CompleteAddress[120];
	char   Gender;
	double Age;
	bool   LivesInASingleParentHome;
};
int main()
{
	Student one;
	strcpy(one.FullName, "Ernestine Waller");
	strcpy(one.CompleteAddress, "824 Larson Drv, Silver Spring, MD 20910");
	one.Gender = 'F';
	one.Age = 16.50;
	one.LivesInASingleParentHome = true;
	ofstream ofs("fifthgrade.ros", ios::binary);
	ofs.write((char *)&one, sizeof(one));
	return 0;
}

and than read it, it is working perfectly i can read all the stored values. I need to store them in a file so that i can read them in another cpp file or ros node, since i am using ros. and these is for object recognition purpose here the map object will contain pattern of objects surface.So is their any method to remove the error or any other way to store a map in a file so to read it in another cpp file. When i dont write the data inside a file and directly read the value of object in same program than the output is coming properly.
Last edited on
Topic archived. No new replies allowed.