I'm trying to edit out the semicolons in a matrix that I'm receiving as an input file and I just can't seem to make any progress. I've used while loops in main to determine the dimensions of the matrix because the program needs to be able to handle matrices of different sizes. I do know that it will always be a square matrix. I think my problem is in the read_file
function. I can read in the matrix from the file and printf it to the terminal but I can't remove the semicolons. I've tried several different ways, including strtok
, but I always end up with junk data. The following code will read in the input file and printf to the terminal without errors or warnings, but it includes the semicolons that I need to remove. Here is my code:
void read_file(int *rows_p, int *columns_p, char matrix[*rows_p][*rows_p], char *file[]){
FILE *file_in;
file_in = fopen(file[1], "r");
/*Please not that the next few declarations and nested loops are commented out. I thought I should show this failed attempt.*/
/*char temp[*rows_p];
int new_col = (*columns_p + 1) / 2;
int i = 0;
int j;
int k;
while(fgets(temp, *columns_p, file_in) != NULL){
int ii = 0;
for(j = 0; j < *columns_p; j += 2){
if((temp[i] == '1') || (temp[i] == '0')){
matrix[i][ii] = temp[j];
ii++;
}
}
printf("%s", matrix[i]);
i++;
}
printf("\n");
*/
char temp[*rows_p][*columns_p];
int i = 0;
int j;
int k;
while(fgets(temp[i], *columns_p, file_in) != NULL){
for(j = 0; j < *columns_p; j++){
if((temp[i][j] == '1') || (temp[i][j] == '0')){
strcpy(matrix[i], temp[i]);
}
}
printf("%s", matrix[i]);
i++;
}
fclose(file_in);
}
And here is what is in the input file (the input file is a .csv):
0;0;0;0;0;0
0;0;0;1;0;0
0;1;0;1;0;0
0;0;1;1;0;0
0;0;0;0;0;0
0;0;0;0;0;0
This will eventually be for a Game of Life program but I find myself stuck not being able to remove the semicolons. I've done similar edits before but I just can't seem to figure this one out. Assistance would be greatly appreciated.
The output that I want to achieve is,
000000
000100
010100
001100
000000
000000
I'm expecting this to be the easiest form to manipulate the matrix in.