I am trying to pass characters from a .txt file to an array (square array, that is, the size of the rows is the same as the columns) where the first line of the file indicates the order of the array that will always be square and then the characters come. The code I made prints the characters but if I try to enter a specific character in the matrix it does not work, I know there is an error but I do not know how to correct it, I hope you can help me.
Example of the file with which I am testing:
5
_ _ # _ _
_ # # # _
_ _ # _ _
_ _ # _ _
_ # # # _
My code:
#include <stdio.h>
int main() {
FILE *fichero;
int c;
const int dimension;
fichero = fopen("origen.txt","r");
if (fichero == NULL) {
printf( "No se puede abrir el fichero.\n" );
}
while (feof(fichero) == 0) //Aquí saco lo de la primera linea
break; //para crear la matriz
fscanf(fichero, "%d", &dimension);
char matriz[dimension][dimension];
int i;
int j;
do {
for (int j = 0; j < dimension; ++j) { //Aqui voy guardando el contenido
c = fscanf(fichero,"%s\n", &matriz[i][j]);//en la matriz
}
if (c != EOF) //pero no es correcto
printf("\n%s", matriz); //me imprime el contenido
} while (c != EOF);while (c != EOF);//pero no de forma correcta
fclose(fichero);
return 0;
}
Although it prints the contents of the file, when trying to print, for example, matrix [0] [0] does not work.