I am quite new to C programming and I have been set the task of reading a .csv file containing a 2d integer array, I need to then print it so it shows up in the terminal. The array is a 4 row by 2 col integer array, but when I use this code I get a random list of numbers. I have attached the code I used and the .csv file array and how it should look like.
#include <stdlib.h> #include <stdio.h> int main() { FILE *in_data = fopen("in.csv", "r"); int i; int j; int trIn[4][2]; if(in_data == NULL) { printf("error\n"); return 1; } for(i = 0; i < 4; i++){ for(j = 0; j < 2; j++){ char junk; if (j != 0) fgetc(in_data); fscanf(in_data, "%c%d%c", &junk, &trIn[i][j], &junk); printf("%d ", trIn[i][j]); } fgetc(in_data); printf("\n"); } return 0; } .csv file array:
0 0 0 1 1 0 1 1 .csv file (raw):
"0","0" "0","1" "1","0" "1","1"
fgetcparses one character at a time, including spaces and newline characters.