I need to read a large binary file (~1GB) into a std::vector<double>
. I'm currently using infile.read
to copy the whole thing into a char *
buffer (shown below) and I currently plan to convert the whole thing into doubles
with reinterpret_cast
. surely there must be a way to just put the doubles
straight into the vector
?
I'm also not sure about the format of the binary file, the data was produced in python so it's probably all floats
ifstream infile(filename, std--ifstream--binary);
infile.seekg(0, infile.end); //N is the total number of doubles
N = infile.tellg();
infile.seekg(0, infile.beg);
char * buffer = new char[N];
infile.read(buffer, N);
Use the fread Function to Read Binary File in C FILE* streams are retrieved by the fopen function, which takes the file path as the string constant and the mode to open them. The mode of the file specifies whether to open a file for reading, writing or appending.
The BinaryReader class is used to read binary data from a file. A BinaryReader object is created by passing a FileStream object to its constructor.
Binary files are more efficient because they use less disk space and because you do not need to convert data to and from a text representation when you store and retrieve data. A binary file can represent 256 values in 1 byte of disk space.
Assuming the entire file is double, otherwise this wont work properly.
std::vector<double> buf(N / sizeof(double));// reserve space for N/8 doubles
infile.read(reinterpret_cast<char*>(buf.data()), buf.size()*sizeof(double)); // or &buf[0] for C++98
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With