How to read/read arrays of structs in binary mode.

Hi guys, I'm teaching myself read/write in binary mode and am having some difficulty when reading/writing arrays of structs. After I've written data to a file, I read it with another program but I get ridiculous numbers instead of what I originally wrote to it.

Here's how I write to the file:
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct SalesData
{
	string division;
	double quarterlySales;
};

void writedata_tofile(SalesData[]);

int main()
{
	SalesData qsales[4];

	for(int index = 0; index < 4; index++)
	{
		cout << "Quarter " << index+1 << endl;
		cout << "Division? ";
		cin >> qsales[index].division;
		cout << "Sales? ";
		cin >> qsales[index].quarterlySales;
	}
	writedata_tofile(qsales);
	cout << "Data Processed." << endl;

	return 0;
}

void writedata_tofile(SalesData data[])
{
	fstream salesfile("salesdata.dat", ios::out | ios::binary);
	salesfile.write(reinterpret_cast<char *>(&data), sizeof(data));
	salesfile.close();
}

Then I read like 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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct SalesData
{
	string division;
	double quarterlysales;
};

void displaydata(SalesData[]);

int main()
{
	fstream corp_datafile;
	SalesData qsales[4];
	string filename;

	do{
		cout << "What file to read data from? ";
		cin >> filename;
		corp_datafile.open(filename.c_str(), ios::in | ios::binary);
		if(corp_datafile.fail())
			cout << "There was a problem opening the file. Maybe a bad file name.\n";
	}while(corp_datafile.fail());
	corp_datafile.read(reinterpret_cast<char *>(&qsales), sizeof(qsales));
	displaydata(qsales);
	return 0;
}

void displaydata(SalesData thedata[])
{
	cout << "[SALES DATA]\n";
	for(int index = 0; index < 4; index++)
	{
		cout << "QUARTER:      " << index+1 << endl;
		cout << "Division: " << thedata[index].division << endl;
		cout << "Sales:" << thedata[index].quarterlysales << endl;
	}
}

I was thinking I'm doing the file.read/write lines wrong but I dunno why they're wrong.
Thanks for any help.
Hi!

In write code I see some problem:

The writedata_tofile function save only one record, not all. You should use a loop. The other problem that sizeof(SalesData) doesn't reflect the real size of the record because the string is an object. If you save to file an object then you will save only adress of object. Not the members.

So you should convert the data of string to array o fchar with the help of c_str() and you can query the size of array.

The corp_datafile.read function has similar problem as above.
Last edited on
Alright, did that and everything is working fine now. :-) I'd post the code but I'm not at my computer at the moment.

I'll be sure to not to make that mistake in the future either >.>
Last edited on
Topic archived. No new replies allowed.