Need help understanding how vectors are represented in binary [C++] -
this question has answer here:
- serialise , deserialise vector in binary 4 answers
i'm trying learn how crack file formats, started simple example i've taken there: how read / write struct in binary files?
#include <fstream> #include <iostream> #include <vector> #include <string.h> using namespace std; typedef struct student { char name[10]; double age; vector<int> grades; }student_t; void readbinaryfile(string filename) { ifstream input_file(filename, ios::binary); student_t master[3]; input_file.read((char*)&master, sizeof(master)); (size_t idx = 0; idx < 3; idx++) { // if wanted search specific records, // should here! if (idx == 2) ... cout << "record #" << idx << endl; //cout << "capacity: " << master[idx].grades.capacity() << endl; cout << "name: " << master[idx].name << endl; cout << "age: " << master[idx].age << endl; cout << "grades: " << endl; (size_t = 0; < master[idx].grades.size(); i++) cout << master[idx].grades[i] << " "; cout << endl << endl; } input_file.close(); } int main() { student_t apprentice[3]; strcpy(apprentice[0].name, "john"); apprentice[0].age = 21; apprentice[1].grades.push_back(1); apprentice[1].grades.push_back(2); apprentice[1].grades.push_back(3); strcpy(apprentice[1].name, "jerry"); apprentice[1].age = 22; apprentice[0].grades.push_back(4); apprentice[0].grades.push_back(5); apprentice[0].grades.push_back(6); strcpy(apprentice[2].name, "jimmy"); apprentice[2].age = 24; apprentice[2].grades.push_back(7); apprentice[2].grades.push_back(8); apprentice[2].grades.push_back(9); string filename = "students2.data"; // serializing struct student.data ofstream output_file(filename, ios::binary); output_file.write((char*)&apprentice, sizeof(apprentice)); output_file.close(); // reading readbinaryfile(filename); system("pause"); return 0; }
i can write file , read correctly, , when open in hex editor, this:
6a 6f 68 6e 00 cc cc cc cc cc cc cc cc cc cc cc 00 00 00 00 00 00 35 40 40 59 84 00 80 cf 84 00 8c cf 84 00 8c cf 84 00 6a 65 72 72 79 00 cc cc cc cc cc cc cc cc cc cc 00 00 00 00 00 00 36 40 50 85 84 00 60 d0 84 00 6c d0 84 00 6c d0 84 00 6a 69 6d 6d 79 00 cc cc cc cc cc cc cc cc cc cc 00 00 00 00 00 00 38 40 50 79 84 00 b8 cf 84 00 c4 cf 84 00 c4 cf 84 00
i can find names (6a 6f 68 6e 00 cc cc cc cc cc), , ages (00 00 00 00 00 00 35 40), have lot more trouble finding grades values. thought making second files different values find differences, found don't understand instead. changing first student to:
strcpy(apprentice[0].name, "john"); apprentice[0].age = 21; apprentice[1].grades.push_back(1); apprentice[1].grades.push_back(2); apprentice[1].grades.push_back(3); apprentice[1].grades.push_back(4); apprentice[1].grades.push_back(5); apprentice[1].grades.push_back(6);
i expected bigger file, size doesn't change:
6a 6f 68 6e 00 cc cc cc cc cc cc cc cc cc cc cc 00 00 00 00 00 00 35 40 40 59 8e 00 48 cf 8e 00 54 cf 8e 00 54 cf 8e 00 6a 65 72 72 79 00 cc cc cc cc cc cc cc cc cc cc 00 00 00 00 00 00 36 40 50 85 8e 00 88 79 8e 00 ac 79 8e 00 ac 79 8e 00 6a 69 6d 6d 79 00 cc cc cc cc cc cc cc cc cc cc 00 00 00 00 00 00 38 40 50 79 8e 00 98 d0 8e 00 a4 d0 8e 00 a4 d0 8e 00
how possible? tried vector 60+ elements , file still remain same size... appreciated!
edit: tux3 pointed out, i'm not saving vectors binary. should have paid more attention code copied, bad.
output_file.write((char*)&apprentice, sizeof(apprentice));
this not going think. std::vector
puts data on free store, not in object array.
so here you're writing vector's metadata (its size, capacity, pointer data, ...), not data itself.
Comments
Post a Comment