I am working in C++.
I have a large file of data in numpy format. The size of the file is exactly 8,964,000,128 bytes, and it consists of 128 bytes of header data (which I don't care about), followed by a 3000 x 3000 x 249 array of binary floats. (Note that 128 + 3000*3000*249*sizeof(float) = 8,964,000,128, so the file size checks out.)
I want to load the 3000 x 3000 x 249 = 2,241,000,000 float values into a vector. When I try to do that with ifstream::read, it only fills the first 93,516,352 entries of the vector with data, leaving the remaining 2,147,483,648 entries unchanged. I know they are not changed because they remain equal to their initial value of 0, and I happen to know for sure that there are no 0s in the file data. What am I doing wrong? I note that the number of unchanged entries is INT_MAX + 1, which can't be a coincidence. So perhaps there is some kind of overflow occurring somewhere?
Here is a minimal working example that generates this problem:
#include <iostream>
#include <fstream>
#include <vector>
#include <climits>
#include <algorithm>
int main(){
std::ifstream ifs("fine_FA_S16_I000_recon.npy", std::ifstream::binary);
ifs.seekg(128); // Skip header
size_t nx = 3000, ny = 3000, nz = 249;
std::vector<float> data(nx*ny*nz, 0);
ifs.read(reinterpret_cast<char*>(&data[0]), sizeof(float)*nx*ny*nz);
size_t zeroes = std::count(data.begin(), data.end(), 0);
std::cout << "Size of vector = " << data.size() << '\n';
std::cout << "Number of zeroes = " << zeroes << '\n';
std::cout << "INT_MAX = " << INT_MAX << '\n';
return 0;
}
The result I was expecting is:
Size of vector = 2241000000
Number of zeroes = 0
INT_MAX = 2147483647
The result I get is:
Size of vector = 2241000000
Number of zeroes = 2147483648
INT_MAX = 2147483647
I'm sure I'm doing something silly, but I can't see it.