In the following code, I am trying to read a CSV file (in the order: #,#,#,#) into a 2D array. However, I am running into a problem where this line
arry[counter][0] = atoi(numOne);
is not working. In specific, atoi(numOne) is returning 0. However, it prints its int value fine in printf("%s", numOne);
As a result, my array looks like this:
0,1,3,2
0,0,5,4
0,9,8,1
0,10,6,3
With the first item of each row missing.
Why is this?
FILE *fp = fopen(argv[1], "r");
int counter = 0;
char line[150];
while (fgets(line, 150, fp)) {
printf("%s", line);
counter++;
}
int arry[counter-1][4];
NUM_ROWS = counter - 1;
rewind(fp);
//Skip First Line of Var Names
fgets(line, 150, fp);
counter = 0;
while(fgets(line, 150, fp)) {
char *numOne = strtok(line, ",");
char *numTwo = strtok(NULL, ",");
char *numThree = strtok(NULL, ",");
char *numFour = strtok(NULL, ",");
arry[counter][0] = atoi(numOne);
arry[counter][1] = atoi(numTwo);
arry[counter][2] = atoi(numThree);
arry[counter][3] = atoi(numFour);
printf("%s%s%s%s", numOne, numTwo, numThree, numFour);
counter++;
}