I am trying to write and subsequently read a very large array of short data to a file. I am getting a successful write, but on the read, I get a bad bit on rdstate at a seemingly random point during the read long before the end of file. Here is a minimal run-able snippet of my current code:
#include <fstream>
#include <iostream>
int main(int argc, char** argv)
{
short * inData = new short[512 * 512 * 361];
//fill with dummy data
for (int z = 0; z < 361; z++)
{
for (int y = 0; y < 512; y++)
{
for (int x = 0; x < 512; x++)
{
inData[x + y * x + z * y * x] = x + y + z;
}
}
}
//write file
{
std::ofstream outfile("data.bin", std::ios::out, std::ios::binary);
if (outfile.is_open())
{
//header
char buffer[10] = "vol_v1.00";
outfile.write(buffer, 10);
//dimensions
int dims[] = { 512,512,361 };
outfile.write(reinterpret_cast<char*>(dims), std::streamsize(3 * sizeof(int)));
//spacing
double spacing[] = { 0.412,0.412,1 };
outfile.write(reinterpret_cast<char*>(spacing), std::streamsize(3 * sizeof(double)));
//data
outfile.write(reinterpret_cast<const char*>(&inData[0]), sizeof(inData[0]) * dims[0] * dims[1] * dims[2]);
std::cout << "write finished with rdstate: " << outfile.rdstate() << std::endl;
}
}
short * outData = new short[512 * 512 * 361];
//read file
{
std::ifstream infile("data.bin", std::ios::in, std::ios::binary);
if (infile.is_open())
{
// get length of file:
infile.seekg(0, infile.end);
long length = infile.tellg();
infile.seekg(0, infile.beg);
std::cout << "file length: " << length << std::endl;
//header
char buffer[10];
infile.read(reinterpret_cast<char*>(buffer), 10);
std::string header(buffer);
//dimensions
int* dims = new int[3];
infile.read(reinterpret_cast<char*>(dims), std::streamsize(3 * sizeof(int)));
//spacing
double* spacing = new double[3];
infile.read(reinterpret_cast<char*>(spacing), std::streamsize(3 * sizeof(double)));
infile.read(reinterpret_cast<char*>(&outData[0]), std::streamsize(sizeof(outData[0]) * dims[0] * dims[1] * dims[2]));
std::cout << "ending pointer pos: " << infile.tellg() << std::endl;
std::cout << "read finished with rdstate: " << infile.rdstate() << std::endl;
}
}
free(outData);
free(inData);
system("PAUSE");
return 0;
}
I have other attempts where I read in chunks, but for brevity just did the single chunk here as the issue seems the same. But with chunks, the data is read for a few hundred thousand values and then fails with bad bit.
The two calls to fstream are incorrectly made. As written, it still compiles in VS2015 but does not properly set the two flags (write and binary), resulting in the stream error seen above. Below are the correct calls to write and read a binary file:
std::ofstream outfile("data.bin", std::ofstream::out | std::ofstream::binary);
std::ifstream infile("data.bin", std::ofstream::in | std::ofstream::binary);