I want to store my 2D matrix file into the while (fgets), but somehow it didn't store the value correctly. can someone help me check the while(fget) for the matrix is correct please. I'm a newbie with C. Thank you so much.
I've using 2D array to store in Matrix1 and matrix2, but i want read the file by using while (fgets) and store the value into it.
char buff[50000];
char *token;
// Get the input files
FILE* inputMatrix1 = fopen(argv[1], "r");
FILE* inputMatrix2 = fopen(argv[4], "r");
char* p1;
char* p2;
// Get matrix 1's dims
int n_row1 = strtol(argv[2], &p1, 10);
int n_col1 = strtol(argv[3], &p2, 10);
// Get matrix 2's dims
int n_row2 = strtol(argv[5], &p1, 10);
int n_col2 = strtol(argv[6], &p2, 10);
int* matrix1 = malloc((n_row1 * n_col1) *sizeof(int));
int* matrix2 = malloc((n_col2 * n_col2) *sizeof(int));
int* result = malloc ((n_row1 * n_col1)* sizeof(int));
// TODO: Parse the input csv files and fill in the input matrices
while(fgets(buff,50000,inputMatrix1) != NULL){
if (strstr(buff, ",") != NULL){
token = strtok(buff, ",");
int column ;
int row;
for(row = 0; row < n_row1; ++row){
for (column = 0; column < n_col1; ++column){
while (token != NULL){ //If there still tokens
matrix1[row * n_col1 +column] = atoi(token);
token = strtok(NULL, ",");
column++;
}
}row++;
}
}
}