Hello, my problem is this: I have a text file in which a maze is drawn and I have to load it into a two-dimensional array (matrix) to work with it, I tried to use fgets but it does not work as I should and have thought that the best option is to read it character to character but it ends up doing strange things the program even though the compiler does not give any error. The code of the program that I have is the following:
I declare the libraries and the struct with the two-dimensional I'm going to use
#include <stdio.h> #include <stdlib.h> #include <string.h> #define FIL 15 #define COL 30 typedef struct //Declaro el tablero del laberinto como struct dado { //que posteriormente tengo que añadirle mas datos y //asi es mas comoda su manipulacion char Laberinto [FIL][COL] }Tablero;
This is the main of the program
int main() { char fichero[N]; Tablero tab; FILE *f; printf("\n\tIntroduce el nombre del fichero del que desea cargar el tablero. "); gets(fichero); f = fopen(fichero , "r"); ValidarApertura(&*f); CargarTablero(&*f , &tab); getchar(); return 0; }
And the function that I use to load the file in the two-dimensional is the following:
void CargarTablero(FILE *f , Tablero *tab) { char aux; //Aqui ya esta validada la apertura del fichero y int i , j; //lo abre correctamente y el paso de parametros esta correcto while(aux != EOF) { for(i = 0; i < FIL - 1; i++) { for (j = 0; j < COL -1; j++ ) { aux = fgetc(f); tab->Laberinto[i][j] = aux; printf("%c",tab->Laberinto[i][j]); } } } fclose(f); }
The content of the file that I have to read and save in the bidimensionale is this:
##############################
###### ########## E
###### ########## ############
###### ############
########### ##################
########### ##################
##### #######
##### ##### ##### ############
##### ##### ##### ############
## ## ## ##############
##### ##### ##################
##### ##### ##################
##### ##### ##################
########### S
##############################
And when executing it and loading the file, the following appears on the screen:
Any idea how I can do? Thank you very much