I'm extremely sorry. I have forgot to post my code. Here it is...
Code:
#include <stdio.h>
#include <stdlib.h>
struct matrix
{
int nRow;
int nCol;
float **m;
};
typedef struct matrix *data;
data getdata(char file_path_name[])
{
FILE *db;
int ch;
int i, j, cols;
int rows = 0, entries = 0;
data ptrdata=(data)malloc(sizeof(data*)) ;
db = fopen(file_path_name, "r");
printf("\nCollecting information...\n");
while(1)
{
ch=fgetc(db);
if(ch==EOF)
{
//rows++;
//entries++;
break;
}
if(ch=='\n')
{
rows++;
entries++;
}
if(ch==' ')
entries++;
}
fclose(db);
cols = entries/rows;
printf("\nData contains %d rows and %d columns\n", rows, cols);
ptrdata->nRow = rows;
ptrdata->nCol = cols;
db = fopen("data.dat", "r");
ptrdata->m = (float **) malloc(rows * sizeof(float *));
for(i = 0; i < rows; i++)
ptrdata->m[i] = (float *) malloc(cols * sizeof(float));
for(i = 0; i < rows; i++)
for(j = 0; j < cols; j++)
fscanf(db, "%f", &(ptrdata->m[i][j]));
fclose(db);
return ptrdata;
}
int main()
{
char file_path_name[100];
data pb;
int i, j;
printf("Provide name of the file (including path):\n");
gets(file_path_name); //data.dat
pb = getdata(file_path_name);
printf("\nPrinting data matrix...\n\n");
for(i = 0; i < pb->nRow; i++)
//for(i = 0; i < 5; i++)
{
for(j = 0; j < pb->nCol; j++)
//for(j = 0; j < 5; j++)
{
printf("%.2f ",*(*((pb->m)+i)+j));
}
putchar('\n');
}
free(pb);
return 0;
}