I would like to get the numeric values from a .csv file and store them in an array. My attempt can be seen here:
FILE *ifp;
ifp = fopen(DIREC,"r");
if(ifp == NULL){
fprintf(stderr,"Input file not read.\n");
exit(1);
}
int rows = 0;
int columns = 0;
rows = readParts(ifp);
fseek(ifp, 0, SEEK_SET);
columns = readConfigs(ifp);
fseek(ifp, 0, SEEK_SET);
char arr[rows][columns];
arr[rows][columns] = malloc(sizeof(int)*rows*columns);
for (int i=0; i < rows; ++i){
while(!feof(ifp)){
int count = 0;
int c = fgetc(ifp);
if(c == '\n'){
break;
}
if(isdigit(c)){
arr[i][count] = c;
count++;
}
}
}
printf("First entry: %d", arr[0][0]);
fclose(ifp);
However, I am having issues with using fgetc() to read the integer values. I know it's grabbing the numbers as doing printf("%c",c); returns the correct results. However, when assigning c to arr, I am not assigning the correct value as fgetc() does not return the actual character value. I tried casting (int) c, but it did not change anything. I have looked into fnscanf(), but I am not sure how this would work since my .csv file also contains non-numerics. Is there a proper way to assign integer values to an array using fgetc()?