'I have a problem scanning numbers in a csv file into a two dimension vector in c
I have a csv file in this format
28,36,59,10,48,41,58,34,35,72
44,36,40,28,29,91,79,74,29,19
80,75,82,21,97,85,41,77,40,34
83,11,46,91,97,22,17,38,75,83
32,99,19,80,18,26,77,10,66,92
41,65,54,66,67,94,50,13,48,88
89,25,96,64,66,42,91,29,52,51
42,56,23,65,38,10,65,26,87,25
43,15,50,71,16,39,51,19,15,37
92,28,42,39,72,42,85,18,72,14
I am trying to read all that into a two dimension vector with fsanf() but it seems like the conditional structure I have used is returning an exit code, I thought I could take advantage of fscanf function to use to parse for numbers and add them to the array but my code has a problem, please help
Code
FILE* matFile = fopen(argv[1], "r");
if (matFile == NULL){
puts("File does not exist");
return 99;
}
char* p1;
char* p2;
int ROWS = strtol(argv[2], &p1, 10 );
int COLS= strtol(argv[3], &p2, 10 );
int (*matrix_array)[ROWS] = malloc(sizeof(int[ROWS][COLS]));
//here is where I am tryin to use fscanf to capture the numbers with no success
for(int i=0;i<ROWS;i++){
for(int j=0;j<COLS;j++){
if(fscanf(matFile,"%d",&matrix_array[i][j])!=1)
return 99;
}
}
Wy is the fscanf returning false for the csv I just posted?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
