简体   繁体   中英

error when trying to Read vector from file

I am a beginner in c++ and I wrote this code to test if I can write a vector to a binary file and later read the vector from the file. This is the code:

vector <student> stud;
vector <student> temp;

void makeObj (string name, int age)
{
student newstudent (nam, ag);
stud.push_back(newstudent);
}

int main ()
{
makeObj("kwame", 14);
stud[0].print();

makeObj("kojo", 12);
stud[1].print();

makeObj("kwabena", 7);
stud[2].print();

fstream gameSave ("gameSaveFile.txt", ios::binary | ios::out | ios::in);
gameSave.write(  (char *) &stud, sizeof(stud));
gameSave.seekg(0);

gameSave.read( (char *) &temp, sizeof(stud));

temp[0].print();

gameSave.close();
return 0;
}

When I run the code, an unknown error occurs whiles the program is running which I cannot figure out what error that is. At first I thought it might be an error with the casting. So I decided to cast the vector using reinterpret_cast like this: reinterpret_cast< char *>(&stud) but that did not solve the problem. But when I just write the vector to the file without trying to read it, the code runs successfully without any error.

Does someone know what is causing the error in my code when I try to read the vector? and the kind of error I am getting. And what is the correct way that I can write and read vectors from a file without any error.

A binary file if nothing more than a sequence of bytes. A vector of students on the other hand is a complex object internally using pointers: the vector has a pointer to the beginning of its internal array, and as a student seems to have a string attribute, each student object will have a pointer to the internal character array.

You cannot hope that just writing the top level part of the vector will be enough. What you need is serialization that mean a conversion (bi-directional) between a complex object and a byte sequence.

A vector is commonly serialized (in binary form) by:

  • an integer(*) for the length
  • the serialized form for every element of the vector (n items)

Without knowing the exact definition of student class, I cannot say what its serialized form would be.

(*) In this simple example, I assume that you will read the file in the same architecture where you write it. It you wanted the serialized form to be portable across different architecture, you would need to take care of the integer size (4 bytes or ...) and the endianness. But IMHO this will be for another question.

sizeof(stud) just shows you the size that vector occupied in stack and nothing more. Also, pointer of a vector is different from pointer of its internal data. You must cache the pointer of inside data using code somethins like this stud.data() . And I think there is not a way to set internal pointer of a vector to another one, so certainly you could not get the pointer of data of one vector and assign it to another one. I think you can write data inside your vector using code like this(If there is not any pointer in your student class:

gameSave.write((char*)stud.data(), stud.size() * sizeof(student)); // It just write your data not member variables of vecto

You need serialization to do this job in general. If you give your class in detail more help would be possible. But I suggest that first do this job for a simple type like int and then extend it for your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM