reading a columns input data file
I need to read a 3 column input data file and to select (for further calculations) just the first 2 columns...
The problem I have is that when I try to "printf" these data, the program crushes, so I'm not be able to check if my program actually reads corectly the input file...
2.5000000000E+1 1.6285272837E+0 1.2494180202E+0
2.5018749237E+1 1.3162814379E+0 1.9428113699E+0
2.5037498474E+1 1.2828449011E+0 2.7128908634E+0
2.5056249619E+1 1.5575599670E+0 2.9970877171E+0
2.5074998856E+1 1.9759304523E+0 2.5490462780E+0
2.5093750000E+1 2.2521975040E+0 1.6395831108E+0
2.5112499237E+1 2.1530494690E+0 9.4248563051E-1
2.5131248474E+1 1.7716845274E+0 1.0123537779E+0
2.5149999619E+1 1.4909162521E+0 1.7716845274E+0
TO be more, clear, in the first part of my program I read the file line by line (I look for '\n' delimiters), so finally I get the number of lines of the input files... This works fine!
I get lets say, count=373, "count" being the parameter i used for the number of lines...
Then, I just want to select fron the same file, the first two columns, as a arrays, so I wrote:
double *x, *y, *z;
for ( i=0; i<count; i++ )
{ fscanf (stream,"%le /t %le /t %le", &x[i], &y[i], &z[i]);
}
I tried then to check if the file is read correctly, so I added in the fors's above loop the function :
printf ("\n %le", &x[i]);
At this point, the program crushes, so I suppose something it's not correct...
I would appreciate any suggestions or help in order to be able to "printf" the first 2 columns, so I will know my program reads correctly the data file....
Thank you!
reading 3 columns input data file
What I want here is to read a 3 column input data file, as the folloing:
2.5000000000E+1 1.6285272837E+0 1.2494180202E+0
2.5018749237E+1 1.3162814379E+0 1.9428113699E+0
2.5037498474E+1 1.2828449011E+0 2.7128908634E+0
2.5056249619E+1 1.5575599670E+0 2.9970877171E+0
2.5074998856E+1 1.9759304523E+0 2.5490462780E+0
2.5093750000E+1 2.2521975040E+0 1.6395831108E+0
2.5112499237E+1 2.1530494690E+0 9.4248563051E-1
2.5131248474E+1 1.7716845274E+0 1.0123537779E+0
2.5149999619E+1 1.4909162521E+0 1.7716845274E+0
2.5168748856E+1 1.4909162521E+0 2.7529489994E+0
this data file has 373 line, this number being returned by another function of the frogram, which works fine, so I won't put it here...
What I want is to read this file and to use these numbers in further calculations...
Selecting from my program whe part that is not working, this looks as follows:
-----------------------------------------
#include <malloc.h>
#define CALLOC(num, type) ((double*)calloc (num, sizeof(double)))
#define FREE(addr) (free((void*) (addr)))
int i, count;
double *x, *y, *z; // I'll skip the declaration of other
// variables and the other parts
// of the programs (within main func)
int main (void)
{
FILE *stream;
if ( (stream = fopen ( "r42.txt", "r" )) == NULL )
{ printf ("Cannot read the new file\n");
exit (1);
}
count = 373; // returned by another function
// of the program, I have 373 lines
// in each of the 3 columns
x=CALLOC(count, double);
y=CALLOC(count, double);
z=CALLOC(count, double);
for ( i=0; i<count; i++ )
{ fscanf (stream,"%le %le %le", &x[i], &y[i], &z[i]);
printf ("\n %d %le %le %le", i, x[i], y[i], z[i]);
}
FREE(x);
FREE(y);
FREE(z);
fclose (stream);
}
---------------------------------------------------------
the printf function will give me 373 lines as follows:
0.000000e+000 0.000000e+0 0.000000e+0
so, I don't understand why the input file is not read :(
Thank you for your help!