Enter file data in two-dimensional array

1

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

    
asked by Josemanuu 05.05.2017 в 19:14
source

1 answer

5

Hello.

Before moving on to the problem, let me make some recommendations:

1.- You lack a ';' in the part of the declaration of the structure, although it does not give you an error because it has only one data, you should place it:

typedef struct {
   char Laberinto [FIL][COL];
}Tablero;

2.- Do not use the function gets() since it is discontinued, you can use instead scanf() or fgets() ; the difference between these last two is that scanf () reads a string until it finds a line break or a blank space so you can not read a string of type "text string", it will only read "string" with the function fgets() if you can read the complete line, I recommend you look a bit at the documentation of these two functions. In this case, as the name of the file should not contain spaces, use scanf() :

printf("\n\tIntroduce el nombre del fichero del que desea cargar el tablero: ");
scanf("%s", &fichero);

Now if we go with the problem

Although valid with the while() that (aux != EOF) are not doing it within the cycle of repetition of the for() , when adding this validation the problem is solved, you would have something like this:

  while(aux != EOF)
  {
    for(i = 0; i < FIL-1; i++)
    {
      for (j = 0; j < COL-1; j++ )
      {
        aux = fgetc(f);

        if(aux == EOF)
          break;

        tab->Laberinto[i][j] = aux;
        printf("%c",tab->Laberinto[i][j]);

      }
    }
  }

PS: check the indexes of the for() , if you set the condition < I think you should not subtract -1.

I hope I have helped you, regards.

    
answered by 05.05.2017 / 20:02
source