I am working on a program which reads a matrix of integer and inputting them into a 2-D dynamic array, and also finding its dimension. Here is what I made so far :

here is the matrix.dat :Code:FILE *fp; int **array, *subarr; int xsize,ysize,i=0; char n; fp = fopen("matrix.dat", "r"); if(fp == NULL){printf("error\n");return 0;} array = (int **)malloc(sizeof(int *)); array[0] = (int *)malloc(sizeof(int)); xsize=ysize=0; while(!feof(fp)){ fscanf(fp, "%d", &array[xsize][i]); fscanf(fp, "%c", &n); i++; ysize++; array[xsize] = realloc(array[xsize], (i+1) * sizeof(int)); if(n == '\n'){ xsize++; i = 0; array = realloc(array, (xsize+1) * sizeof(int *)); array[xsize] = (int *)malloc(sizeof(int)); } } xsize--; ysize = (ysize-1) / xsize; for(i=0;i<xsize-1;i++){ for(j=0;j<ysize;j++){ printf("%d", array[i][j]); } printf("\n"); }

it gave me xsize = 6 and ysize = 10 instead of 8 X 8,Code:1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 0 0 1 1 1 0 0 0 0 0 1 1 1 1 0 0

It sometimes gave me the right value, but when I change the matrix, it become screwed up. If anyone can give me a hint to where I made mistake with the code, I would appreciate it. Thanks