c++ - read binary file into the address of vector element -
i have binary file contains doubles of size of 8 bytes. want read doubles vector<double>
, see below
ifstream infile("x.dat", ios::binary); vector<double>data(filesize/8, 0.0); for(int i=0; i< (filesize/8); ++i) { infile.read( (char *) (&data[i]), sizeof(double) ); }
i know work if data c
array, not sure if work vector
, since vector
contains more stuff c
array(vector
has methods), address &data[i]
mean address of data member of ith element?
yes, each vector element double
object, , &data[i]
points object.
in fact, double
objects in contiguous array, can read them @ once:
infile.read((char*)data.data(), filesize);
of course, noted below, requires file have been written in format compatible program's binary representation of double
values, otherwise values garbage.
Comments
Post a Comment