In this post, we will see how to resolve Reading and Writing std::vector<struct> to a file
Question:I need to write a
vectorto a file, and then read it back into a
vectorcontains hundreds, possibly thousands, of
structs which each contain 2 different
I thought that the best way to do it would be to get the pointer with
MyVector.data()and somehow write the data using that.
Just in case here’s a diagram of the
I tried multiple methods, such as
fwrite(), but none of them worked.
binary ‘=’: no operator found which takes a right-hand operand of type ‘Frame’ (or there is no acceptable conversion)
std::ostreambuf_iteratorwrites character data to the output stream whenever it is assigned a single character via its
operator=. Since you are specifying
CharTtemplate parameter of
charvalue to be assigned to it (ie, when used to write data from a
std::string, etc), but you are trying to write
Frameobjects from a
std::vector<Frame>instead. IOW, since you are trying to write
charvalues are expected, that is why you are getting the conversion error on
You have a similar issue with reading in using
std::istreambuf_iterator, too. It is meant for reading in character data (via its
operator*), not structured data.
For what you are attempting to do, you need to use
std::istream_iterator) instead. You will simply have to define an overloaded
Frameto write/read its member data to/from a
If you have better answer, please add a comment about this, thank you!