Ho fatto in questa maniera ma a quanto pare il valore di i non viene caricato
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
FILE *fp = fopen("matrix.txt", "r");
if(fp == NULL){
printf("File non trovato!");
return -1;
}
char buffer[1000];
int i=0;
int j=0;
int dim = 0;
float array[5][5];
while (fgets(buffer, sizeof(buffer), fp) != NULL){
char *cp = strtok(buffer, ",");
printf("%s ",cp);
j=0;
while (cp != NULL && dim < 1000){
cp = strtok(NULL, ",");
array[i][j] = atof(cp);
j++;
//printf("%s-",cp);
}
i++;
dim++;
}
printf("\n");
//printf(" i presenti %d\n", i);
//printf(" j presenti %d\n", j);
fclose(fp);
for(i = 0; i < 5; i++){
for (j=0;j<5;j++){
printf ("%.3f ", array[i][j]);
}
printf("\n");
}
return 0;
}
il file di input e cosi riempito
12.5,23.2,34.2,48.2,48.25
112.5,23.2,34.2,48.2,48.24
212.5,23.2,34.2,48.2,48.211
3312.5,23.2,34.2,48.2,48.233
33212.5,233.2,34.21,48.21,48.33
in stampa mi da
12.5 23.2 34.2 48.2 48.25
23.2 34.2 48.2 48.24
23.2 34.2 48.2 48.211
23.2 34.2 48.2 48.233
233.2 34.21 48.21 48.33