reading/writing binary files into/from vectors

Hi, i'm having some trouble writing/reading data into vectors! This is a simplified example of what I'm trying to do, writing a vector of vector of ints into a binary file, and then reading the data back into a similar vector of vector of ints. However when i read the data back in, the values are different. Here's the 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
int main() {

	// create vector of 2 different sized vectors of ints, and print out the data:
	vector<vector<int>> numbers;
	int sizes[2] = {5,10};

	cout << "original data: " << endl;

	for (int a = 0; a < 2; a++) {
		numbers.push_back(vector<int>());
		for (int b = 0; b < 5; b++) {
			if (a==1) {
				numbers[a].push_back(a);
				numbers[a].push_back(a+b);
				cout << numbers.at(a).at(2*b) << " " << numbers.at(a).at(2*b+1) << " ";
			} else {
				numbers[a].push_back(b);
				cout << numbers.at(a).at(b) << " ";
			}
		}
		cout << endl;
	}

	cout << "teamsizes: " << endl << sizes[0] << endl << sizes[1] << endl;
	
	// save numbers/sizes to file:
	cout << "Enter name for save file: " << endl;
	string filename;
	cin >> filename;
	cin.ignore(1, '\n');
	ofstream files;
	files.open(filename + ".data", ios::out | ios::trunc | ios::binary);
	files.write((char*)&sizes, sizeof(sizes));
	files.close();
	files.open(filename + ".data", ios::out | ios::app | ios::binary);
	for (int b = 0; b < 2; b++) {
		files.write((char*)&numbers.at(b), sizes[b]*sizeof(int));
	}
	files.close();

	// variables to load into:
	vector<vector<int>> numbers2;
	int sizes2[2];
	
	// load data from file and print out:
	cout << "Enter name of save file to load: " << endl;
	cin >> filename;
	cin.ignore(1, '\n');	
	ifstream filel;
	filel.open(filename + ".data", ios::in | ios::binary);
	filel.read((char*)&sizes2, sizeof(sizes2));
	cout << "new data: " << endl;
	int temp;
	for (int x = 0; x < 2; x++) {
		numbers2.push_back(vector<int>());
		for (int y = 0; y < sizes2[x]; y++) {
			filel.read((char*)&temp, sizeof(int));
			numbers2[x].push_back(temp);
			cout << numbers2.at(x).at(y) << " ";
		}
		cout << endl;
	}
	filel.close();
	cout << "teamsizes: " << endl << sizes2[0] << endl << sizes2[1] << endl;

}


and the output:

original data:
0 1 2 3 4
1 1 1 2 1 3 1 4 1 5
teamsizes:
5
10
Enter name for save file:
save
Enter name of save file to load:
save
new data:
3372312 4917840 4917860 4917864 -842150451
3375496 3378072 3378112 3378124 -842150451 -33686019 -572662307 1838531600 20134
9657 3376128
teamsizes:
5
10
Press any key to continue . . .


So any ideas where i might be going wrong? thanks!
replace
1
2
3
	for (int b = 0; b < 2; b++) {
		files.write((char*)&numbers.at(b), sizes[b]*sizeof(int));
	}
with

1
2
3
    for (size_t b = 0; b < numbers.size(); b++) {
        files.write((char*)&numbers[b][0], numbers[b].size()*sizeof(int));
    }


2/at replaced by size()/[b] is optional, but taking the address of the right thing is necessary. Your code was taking the address of a vector, while you needed the address of the first element in the vector.
Last edited on
Thank you that is a great help! It seems so simple now, I've been trying to get this save/load thing working for what feels like ages.

Just out of interest, I've not seen size_t before, i assume it works in a similar way to an unsigned int? what are the advantages of using size_t over unsigned int?
Last edited on
Topic archived. No new replies allowed.