I have a file that contains several records of the same size, all in order. I am able to read in the data into a single char array multiple times, but cannot seem to read it into a 2D array. In the below example, there are 1000 sets of 1000 character data.
// This code works.
std::ifstream file("sample.dat", std::ios::binary);
char data[1000];
int data_size = int(sizeof(data));
for (int i = 0; i < 1000; i++)
{
file.read(data, data_size);
// Print the data in the loop.
}
// This code doesn't work.
// Process terminated with status -1073741571
// I read the error was a divide by zero error.
std::ifstream file("sample.dat", std::ios::binary);
char data[1000][1000];
int data_size = int(sizeof(data[0]));
for (int i = 0; i < 1000; i++)
file.read(data[i], data_size);
// All data in matrix, so print as needed.
At line 19 you're creating a megabyte of data on the stack. That's a lot of data. Try making that static instead: staticchar data[1000][1000];
At line 20, i isn't defined. And you should use size_t. So make this size_t data_size = sizeof(data[0]);