I have multiple textfiles with data as follows:
0.000000E+0 0.000000E+0
1.800000E-1 0.000000E+0
4.200000E-1 0.000000E+0
6.950000E-1 0.000000E+0
9.450000E-1 0.000000E+0
1.225000E+0 0.000000E+0
which was generated by a test instrument. The two columns of data are separated by the tab character. I am trying to read this data file into a program for further processing. The code which i wrote is as follows:
#include <stdio.h>
#include <stdlib.h>
float **make_array(int size); //function to make 2D array
int main(int argc, char **argv)
{
FILE *infile;
infile=fopen(argv[1], "r");
//count number of lines in file
char c;
int i=0, numlines=0;
while((c=fgetc(infile))!=EOF)
if(c == '\n') numlines++;
printf("numlines: %d\n", numlines);
float **entry = make_array(numlines);
//read inputfile
for(i=0; i< numlines; i++)
fscanf(infile, " %G\t%G", &entry[0][i], &entry[1][i]);
//verify read values
printf("\n");
for(i=0; i<numlines; i++)
printf("%E\t%E\n", entry[0][i], entry[1][i]);
//clean up before exiting
free(entry[0]);
free(entry[1]);
free(entry);
fclose(infile);
return 0;
}
//create a 2D array of 2 by {size}
float **make_array(int size){
int i;
float **entry = malloc(2*sizeof(float *));
if(entry==NULL) exit(1);
entry[0] = malloc(size*sizeof(float *));
for(i=0;i<size;i++) entry[0][i] == 0.0;
entry[1] = malloc(size*sizeof(float *));
for(i=0;i<size;i++) entry[1][i] == 0.0;
return entry;
}
When i compile and run the program, it prints out the number of lines in the file correctly but the printf statement to verify the data read into the array entry just prints out all zeros, like below:
numlines: 252
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
0.000000E+00 0.000000E+00
I tried different format specifiers for fscanf like %g,%e,%f,%G etc but they all give the same result. Also, i am using Ubuntu 12.04 with GCC 4.6. Any pointers to what’s wrong would be greatly appreciated.
Khadija.
Your initial loop:
read the entire input file (to determine the size of the array). Once you’ve done that, your calls to
fscanfall fail because you’re already at the end of the file.Assuming the input file is seekable (which it should be if it’s a disk file), you can use
rewind()orfseek()to go back to the beginning and read it again.BTW, your use of
%Gand%Eformat specifiers is valid, but%gand%eare more common (and should work in exactly the same way).And you really should check the value of
argcbefore referring toargv[1]; the program will likely blow up if invoked with no command-line arguments. You should also check whetherfopen()succeeded, and thatfscanf()returned 2 to indicate that it successfully read two data items.