Confused about binary file read/write and arrays.

Hello,

I need to save an array of data to a binary file. Something along the lines of this.

1
2
3
4
5
6
7
8
9
10
struct myDataArray {
   float* myFloatArray;
   int iNumOfFloats;
};

struct myFileStruct {
   myDataArray* fArray0;
   myDataArray* fArray1;
   int iNumInArray0, iNumInArray1;
};


Something like that. I can't seem to find really clear examples online because there seems to be so many different ways to write files( I've even spent time mistakenly reading stuff about memory-mapped files ).

The last thing I tried was like this. This always dumps a 1kb file no matter how much data I fill the struct with.

1
2
3
4
5
6
7
8
9
10
void saveData() {
	std::fstream nxg(sNXGFile.c_str(), std::ios::out | std::ios::binary);
	char* mem = new char[sizeof(nxgFile)];
	mem       = (char*)&nxgFile;
	nxg.write(reinterpret_cast<char*>(&nxgFile), sizeof(nxgStruct));
	nxg.close();

	std::cout << " " << std::endl;
	std::cout << sNXGFile.c_str() << " Stored." << std::endl;
}


I've been trying different things for a couple weeks now. I'm utterly confused and lost. Could anybody give me some advice?

Thanks.
Lines 3 and 4 arn't needed. It looks like you're reserving some memory for your object and then reassigning the mem pointer to the object leaving the fresh memory in the ether.

Just try
nxg.write((char*)&nxgFile, sizeof(nxgStruct));
should work off the top of my head.

The saved data will always be 1K as that is the size of the memory required by all the member variables in your structure ngxStruct.

This may help also:
http://www.functionx.com/cpp/articles/serialization.htm

For the arrays that you create and point to you'll need to iterate through your arrays and write each byte, or write the arrays starting at nxgFile->fArray pointer for the number of elements in the array times the size of an element..

You would be best off writing a member functions for each struct that outputs/inputs the data, passing in a fstream as the parameter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct myDataArray {
   int iNumOfFloats;
   float* myFloatArray;
   void output(fstreamhandle)
  {
     fstreamhandle.write(iNumOfFloats,sizeof(int));
     fstreamhandle.write(myFloatArray,sizeof(float)*iNumOfFloats);
  }
   void input(fstreamhandle)
  {
     fstreamhandle.read(&iNumOfFloats,sizeof(int));
     myFloatArray = new float[iNumOfFloats+1];
     fstreamhandle.read(&myFloatArray,sizeof(float)*iNumOfFloats);
  }
};

if you get the idea?
Last edited on
Hey moooce,

I thought I understood. But I guess not as it crashes out on line 547 of iosfwd.

Here is the new code.

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
struct nxaNode {
public:
	int* iVertex;
	int* iIndex;
	
	int iNumOfVerts, iNumOfIndex;

	void output(std::fstream& f) {
		f.write((char*)iNumOfVerts, sizeof(int));
		f.write((char*)iVertex, sizeof(int)*iNumOfVerts);

		f.write((char*)iNumOfIndex, sizeof(int));
		f.write((char*)iIndex, sizeof(int)*iNumOfIndex);
	}

	void input(std::fstream& f) {
		f.read((char*)&iNumOfVerts, sizeof(int));
		f.read((char*)&iVertex, sizeof(int)*iNumOfVerts);

		f.read((char*)&iNumOfIndex, sizeof(int));
		f.read((char*)&iIndex, sizeof(int)*iNumOfIndex);
	}
};

struct nxaFile {
public:
	nxaNode* nodes;
	int iNumOfNodes;

	void output(std::fstream& f) {
		f.write((char*)iNumOfNodes, sizeof(int));
		f.write((char*)nodes, sizeof(nxaNode)*iNumOfNodes);

		for(int i = 0; i < iNumOfNodes; i++) {
			nodes[i].output(f);
		}
	}

	void input(std::fstream& f) {
		f.read((char*)&iNumOfNodes, sizeof(int));
		nodes = new nxaNode[iNumOfNodes + 1];
		f.read((char*)&nodes, sizeof(nxaNode)*iNumOfNodes);

		for(int i = 0; i < iNumOfNodes; i++) {
			nodes[i].input(f);
		}
	}
};

nxaFile theNXA;

void save() {
	std::fstream f("test.nxa", std::ios::out | std::ios::binary);
	theNXA.output(f);
	f.close();
}

int main(int argc, char* argv[])
{
	std::cout << "Dumping file..." << std::endl;

	theNXA.iNumOfNodes = 10;
	theNXA.nodes       = new nxaNode[10];

	for(int i = 0; i < 10; i++) {
		theNXA.nodes[i].iNumOfIndex = 10;
		theNXA.nodes[i].iNumOfVerts = 10;

		theNXA.nodes[i].iVertex     = new int[10];
		theNXA.nodes[i].iIndex      = new int[10];

		for(int x = 0; x < 10; x++) {
			theNXA.nodes[i].iVertex[x] = x;
			theNXA.nodes[i].iIndex[x]  = x + x;
		}
	}

	save();

	for(int i = 0; i < 10; i++) {
		delete[] theNXA.nodes[i].iIndex;
		delete[] theNXA.nodes[i].iVertex;
	}

	delete[] theNXA.nodes;

	return 0;
}


The line it bottles out on in is.

1
2
3
4
	static int_type __CLRCALL_OR_CDECL to_int_type(const _Elem& _Ch)
		{	// convert character to metacharacter
		return ((unsigned char)_Ch);
		}


I really have no idea what that means.

Thanks.
Nevermind, I was forgetting to address &.

Thank you moooce. :D
Topic archived. No new replies allowed.