I have a simple program that tries to read 2-dimensional data into a heap allocated array of floating-points. The program is as follows.
#include <iostream>
#include <fstream>
void read_file(std::ifstream &inFile, float **arr, unsigned m, unsigned n)
{
for(unsigned i = 0; i < m; ++i) {
for(unsigned j = 0; j < n; ++j) {
inFile >> arr[i][j];
std::cout << arr[i][j];
}
std::cout << std::endl;
}
}
int main() {
const unsigned m = 20;
const unsigned n = 1660;
float **A = new float *[m]; // alloc pointers to rows 1,...,m
for(unsigned row = 0; row < m; row++) A[row] = new float [n]; // alloc columns for each row pointer
std::ifstream inFile("data.dat");
read_file(inFile, A, m, n);
// DEALLOC MEMORY
for(unsigned row = 0; row < m; row++) delete [] A[row]; // dealloc columns for each row pointer
delete [] A; // dealloc row pointers
return EXIT_SUCCESS;
}
The data is a table of 0-1 entries (see here: data) which is nicely row orientated and has 20 rows and 1660 columns. I added the printing into the read_file
function to see what goes wrong and it prints only zeros, but the correct amount at least (20*1660 zeros).
The data seems to be tab delimitered; is that the problem or is my approach completely invalid?
It's exactly what may happen if the file doesn't exist.
You should check that the file exists after creating the
inFile
object, for instance like this:If file doesn't exist,
cin
doesn't put data in your array, and there are chances that you get 0 all the way (I had 0 + other strange stuff), so to sum it up undefined behaviourNote that if the file exists, your program works as expected. It would be even better to check the file after having read a value to be sure that the file has as many values as expected: