Hi,
I tried to get my program to count the number of rows in the input data file and then output back the same data with 7 columns and however many rows. It's giving me all zeros in the output file though, which isn't supposed to happens since the input file is just random numbers, e.g.:
123.45 111.23 33.45 66.345 43545.54 543.54 5435.43
Could someone take a look at the code?
Code:
int j , i;
int rows = 1;
float num;
int c;
while ((c = fgetc(fp)) != EOF)
{
if (c == '\n')
{
rows++;
printf("%d\n", rows);
}
}
float **data;
data = (float**) malloc(rows * sizeof(int));
for (int i = 0; i < rows; i++)
{data[i] = (float*)malloc(7*sizeof(int));
}
//declares array with 7 columns and number of rows in file
for (j=0; j< rows; j++) //repeats for max number of columns
{
for (i=0; i< 7; i++)
{
fscanf(fp, "%f", &num);
data[j][i] = num;
}
}
for(j = 0; j < rows; j++) //repeats for max number of columns
{
for (i=0; i< 7; i++)
{
num = data [j][i];
fprintf(fp2, "%f ", num);
}
fprintf(fp2, "\n");
}
Thanks.