I have successfully followed the answer posted here to write a structure (of type image_info_t
) to a file.
I repeat the process in a loop for N
number of image_info_t
's and all the data is serialized and added to the file correctly.
I now need to read the file, but I need to be able to read an arbitrary number, M
, image_info_t
structs to read from the file (all in order). The answer referenced above explicitly hardcodes the number of structures to read back from the file (i.e., student_t master[3];
). However, I need this number to be dynamic.
I have read here that "C++ standard requires that arrays use either an integer literal or a integer constant when declaring its size. Use <vector>
instead"
My question is: how can I do this? How can I read the set of image_info_t
structs back from the file into a std::vector
?
Here is my current (non-working) code that I am using to read the image_info_t
data back from the file.
std::ifstream input_file(path, std::ios::binary);
const int kpts_size = kpts.size();
feature_t master[kpts_size]; //DOES NOT WORK. If I change to `feature_t master[10];` it works.
input_file.read((char*)&master, sizeof(master));
input_file.close();
Note: this is not an access violation question, and is not related to the "Possible dup" answer. When you tag it as such, people stop reading my question which certainly doesn't help anyone.