MorganGreylock
Programmer
I have a text file that I'm trying to read into an array. The file consists of, on the first line, the # of lines, then followed by data values, 4 per line.
so like this:
5
1.0 2.0 6.0 7.0
5.0 3.0 2.0 5.0
1.0 2.0 6.0 7.0
2.0 9.0 1.0 2.4
5.0 2.0 6.5 1.4
I want to read this data in an X by 4 array, where X is the value on the first line (here its 5, but it could be anything). I've successfully opened the file for read, read in the first line (which I call data_size), and now i'm trying to dynamically allocate an array of size (in this case 5x4) to hold this data, and it's giving me problems. Here is what I have so far:
in the main declaration segment:
later:
Something's not working, and quite honestly I'm not too sure where. I know the inital fopen and first fscanf is working, but the rest of it is really suspect.
Any help is very much appreciated. I've searched for solutions, but nothing really fit my particular situation that I could find.
Thanks in advance,
MG
so like this:
5
1.0 2.0 6.0 7.0
5.0 3.0 2.0 5.0
1.0 2.0 6.0 7.0
2.0 9.0 1.0 2.4
5.0 2.0 6.5 1.4
I want to read this data in an X by 4 array, where X is the value on the first line (here its 5, but it could be anything). I've successfully opened the file for read, read in the first line (which I call data_size), and now i'm trying to dynamically allocate an array of size (in this case 5x4) to hold this data, and it's giving me problems. Here is what I have so far:
in the main declaration segment:
Code:
// ... other declarations
FILE *stream;
float *input_data = NULL;
later:
Code:
void read_input() {
int i;
i = 0;
stream = fopen("data.txt","r");
if( stream == NULL )
printf( "The file data.txt was not opened\n" );
else
{
fscanf(stream,"%i",&data_size);
printf("data size = %d\n",data_size);
input_data = new float[data_size][4];
while(fscanf(stream,”%f %f %f %f”, &input_data[i][0],&input_data[i][1],&input_data[i][2],&input_data[i][3]) != EOF)
{
i++;
}
fclose(stream);
}
}
Something's not working, and quite honestly I'm not too sure where. I know the inital fopen and first fscanf is working, but the rest of it is really suspect.
Any help is very much appreciated. I've searched for solutions, but nothing really fit my particular situation that I could find.
Thanks in advance,
MG