-
February 18th, 2009, 05:25 PM
#1
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");
}
-
February 18th, 2009, 06:33 PM
#2
Re: Reading huge matrices from text files
The capitalized "Long" is throwing me a bit. I didn't think that was a type. Second, "long float"? Don't you just mean double?
%lf (lowercase L) is the correct format specifier to read a double. If you want to just read floats then drop the l entirely.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|