Debo leer la información contenida dentro de un archivo externo en formato .csv y debo guardar cada fila del archivo en un arreglo. El código que tengo para realizar esa acción es el siguiente:
int generate_matrix(char *filename){
FILE *input1 = fopen(filename,"r");
if(input1 == NULL){
return 1;
}
//counts the number of rows the file contains
int rows = 1;
char c = fgetc(input1);
while(!feof(input1)){
if(c == '\n') rows++;
c = fgetc(input1);
}
fclose(input1);
FILE *input2 = fopen(filename,"r");
char *students[rows];
char row[1000];
for(int i = 0; i < rows; i++){
fgets(row,1000,input2);
row[strlen(row)] = '\0';
students[i] = row;
printf("%s", students[i]);
}
fclose(input2);
printf("\n\n");
for(int i = 0; i < rows; i++){
printf("%s\n", students[i]);
}
return 0;
}
Este código es el que se supone que tendría que leer la linea de turno en el archivo y guardarlo en la posición correspondiente del arreglo:
char *students[rows];
char row[1000];
for(int i = 0; i < rows; i++){
fgets(row,1000,input2);
row[strlen(row)] = '\0';
students[i] = row;
printf("%s", students[i]);
}
Al final de cada vuelta de bucle imprimo el valor que toma studenst[i] y efectivamente va leyendo línea por linea y aparentemente el código hace su trabajo. Esta es la salida del programa hasta ese momento:
,TheGodfather,TokyoDrift,SpiritedAway,Tenet,TheGrandBudapestHotel,Frozen
ph20003,0,1,0,1,1,0
fc20015,1,1,0,1,0,0
cm19086,1,1,1,0,0,1
ve20008,0,1,0,1,0,0
mf19026,1,1,1,0,1,1
rc19106,0,0,1,1,0,1
ag19101,0,0,0,0,0,0
pg19065,0,1,0,0,0,0
lr19047,1,1,1,0,0,1
rf18025,0,1,0,0,0,0
aa19091,1,0,1,1,0,1
cc19089,0,1,1,1,0,0
hs20006,1,1,1,1,1,1
av19023,0,1,1,0,0,1
ft19004,0,0,0,0,0,1
dg17019,1,1,0,0,0,1
aa20047,0,1,0,0,0,0
rc19102,1,1,0,0,0,1
gr20035,1,0,1,0,0,0
rm19021,0,0,0,0,0,1
tq19002,0,1,0,0,0,1
ag17057,0,0,1,0,0,1
pc19060,1,1,1,0,0,1
hr18024,0,1,1,0,0,1
pm20072,1,1,0,0,0,1
tc20006,0,1,1,1,0,1
va17017,0,1,0,0,0,1
am18007,1,0,1,0,0,0
sm18038,0,1,0,0,0,0
cc20019,0,0,1,0,0,1
ff18023,0,1,0,0,0,0
vc17044,0,1,0,0,0,0
jm17015,0,1,0,1,0,1
ha19027,0,1,0,1,0,0
cd20017,0,1,1,0,0,1
mp19034,1,1,0,1,0,0
le19005,0,0,0,0,0,0
ma19074,0,1,0,0,0,1
cf20014,0,1,0,0,0,0
am19139,0,1,0,0,0,1
qj20001,0,0,0,0,0,0
pa19031,0,1,0,0,0,1
gs20027,0,1,1,0,0,1
ot20006,0,1,1,1,1,1
ze17002,0,1,1,0,0,1
ga19038,0,1,0,0,0,0
mh19029,0,1,0,0,0,1
fb19009,1,1,1,0,1,0
om20022,0,0,0,0,0,0
jg11008,0,1,1,0,0,0
gr12043,0,1,1,0,0,0
ar14078,0,1,1,0,0,1
vc20009,0,1,0,0,0,1
fa20011,0,1,0,0,0,0
mh19062,0,0,0,0,0,1
pr20028,0,0,0,0,1,1
ha17010,0,1,0,0,0,0
er20001,0,1,0,1,1,1
bc18029,1,1,0,0,0,0
El problema es que cuando recorro al final el bucle para visualizar la información, me imprime información repetida, como si sólo hubiese leído un único registro.
for(int i = 0; i < rows; i++){
printf("%s\n", students[i]);
}
El resultado de lo anterior es:
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
bc18029,1,1,0,0,0,0
Corresponde la último registro del archivo. No sé porqué se está comportando de esa manera, no sé si sea algo que desconozco de la función fgets o que. Agradezco su ayuda