Reading huge matrices from text files
I have been trying to read real numbers of the following format:
-324.5 21.8
23.45 -223.8
...
two columns and quite a lot of rows. The code I have been using to extract it, is pasted here, only generates absurd values in the variable "Matrix". Please let me know what the problem could be. I am sure its some blunder I cant see. Thanks a lot!!!
Heres the code:
#include <iostream>
#include <fstream>
#include <math.h>
#include <stdio.h>
using namespace std;
void main()
{
Long float Matrix[39][2];
int col, row, i, j;
FILE *ftp;
col = 1; row=39;
ftp = fopen("data.txt","r");
for(i = 0; i < row; i++)
for(j = 0; j < col+1; j++){
fscanf(ftp, "%Lf", &Matrix[i][j]);
printf("matrix[%d][%d] = %f\n", i,j,Matrix[i][j]);
}
system("pause");
}
Re: Reading huge matrices from text files
I'm not sure that you format specifications are correct. Since you are
already including <iostream> and <fstream>, I would suggest that
you use them.
Code:
ifstream in("data.txt");
for(i = 0; i < row; i++)
{
for(j = 0; j < col+1; j++)
{
in >> Matrix[i][j];
cout << "Matrix[" << i << "][" << j << "] = " << Matrix[i][j] << "\n";
}
}