I`m having problems converting a char array read from file to an int array. Maybe someone can help me. This is my code:
char vectorPatron[67];
int iPatrones[67];
archivo = fopen("1_0.txt", "r");
for(i=0;i<67;i++){
fscanf(archivo, "%c", &vectorPatron[i]);
printf("%c",vectorPatron[i]);
}
fclose(archivo);
for(i=0;i<67;i++){
iPatrones[i] = atoi(&vectorPatron[i]);
printf("%d",iPatrones[i]);
}
That’s because
atoireceives a null-delimited string, while you are passing it a pointer to a singlechar(both are essentiallychar *, but the intent and use are different).Replace the call to
atoiwithiPatrons[i] = vectorPatron[i] - '0';Also, you can remove the
vectorPatronsarray, simply read into a singlecharin the first loop and then assign to the appropriate place in theiPatronsarray.