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?