Reading from a file- problem with fscanf
Nov 19, 2012 at 4:01pm UTC
Hello I am trying to read numbes from a file and store them to a dynamic array, but the code reads correctly only the first one, but not the rest. In fact, it shows that the rest numbers are equal to 0. what could be the problem?
Here's the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(){
long double **A;
int i,j;
FILE *f;
f=fopen("a-data.txt" ,"r" );
if (f==NULL){
printf("No such file\n" );
exit(0);
}
A=(long double **)malloc(128*sizeof (long double *));
for (i=0;i<128;i++){
A[i]=(long double *)malloc(128*sizeof (long double ));
}
for (i=0;i<128;i++){
for (j=0;j<128;j++){
fscanf(f,"%Lf" ,&A[i][j]);
}
}
for (i=0;i<128;i++){
for (j=0;j<1;j++){
printf("%0.30Lf " ,A[i][j]);
}
printf("\n" );
}
fclose(f);
for (i=0;i<128;i++){
free(A[i]);
}
free(A);
}
and here's the output of the first column of the array
0.212206590789193820002517948398
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
0.000000000000000000000000000000
Last edited on Nov 19, 2012 at 4:24pm UTC
Topic archived. No new replies allowed.