I found the following code which reads a file containing floats as binary:
#include <fstream>
#include <vector>
// ...
// Open the stream
std::ifstream is("input.dat");
// Determine the file length
is.seekg(0, std::ios_base::end);
std::size_t size=is.tellg();
is.seekg(0, std::ios_base::beg);
// Create a vector to store the data
std::vector<float> v(size/sizeof(float));
// Load the data
is.read((char*) &v[0], size);
// Close the file
is.close();
at this stack overflow answer
What I am a little uncertain of is the line: is.seekg(0, std::ios_base::beg);
which is supposed to offset the input position from 0 by std::ios_base::beg
if I have understood it correctly. But isn’t std::ios_base::beg
always 0 since it refers to the beginning of the stream? Meaning is.seekg(0)
or is.seekg(std::ios_base::beg)
would have achieved the same but simpler.