Passing information from a file to an array

2

I have a problem, I want to receive a file and copy the information from this file to an array that I have allocated:

Content that exists inside the file:

2..E.5..A......D
....1E....7B80..
F.8.A.......C.4.
AC...3.F...E.61.
.3...C.45..2..BF
.D9.5..31.A..E0.
E5A...9..0..7C..
...C6..B.E..5.2.
.9......ED0F...C
.E...8.DC..A..F7
....EB7...9.....
5....FAC......D.
.6..C7B9....F.5.
8.........D.94.2
..2..D1...C63..B
.7..8..6.BF..D..

And I want to copy it to a 16 by 16 matrix that I created, and the function that does this is here:

char** lerMatriz(char ** tabuleiro, char * arquivo, int l, int c)
{
    char** tabuleiroP;
    int i, j;
    char carac;
    FILE* p;

    p = fopen(arquivo, "r");

    for(i = 0 ; i < 16; i++)
    {
        for(j = 0; j < 16; j++)
        {
            tabuleiroP = fgets(tabuleiro, 256, p);
        }
    }

    for(i = 0; i < 16; i ++)
    {
        for(j = 0; j < 16; j++)
        {
            printf("%c", tabuleiro[i][j]);
        }
        printf("\n");
    }

    fclose(p);
}

Being my char** tabuleiro my array 16 x 16, char* arquivo my string that contains the file name and int l and int c the number of rows and columns in the array (16 x 16).

What would be the problem in my code?

    
asked by anonymous 21.06.2015 / 04:09

1 answer

0

You have several problems with your code. I'll tell you what I found but I can not guarantee it's an exhaustive list.

First of all do not just read 256 characters from the entry and play straight into the array. You're forgetting line breaks ("\ n")

Secondly, l and c are always 16 or can be other numbers as well? If it is always 16 you do not need to pass them as a parameter and if it is not always 16 you should be using those limits instead of 16 in your loops.

Anyway, a more descriptive name would be useful. For example, ncol instead of c .

From now on I'm going to assume that it's always 16, because then we can use static allocation, which simplifies things.

A third problem is that what your job does is unclear, that is: what it gets and what it returns. What is the difference between tray and tray? Because you said that the function returns a char** but does not use any return ?

A very important fourth problem is that you are not allocating storage space for your array. Simply declaring a pointer does not allocate space for a vector. Furthermore, char ** is not equivalent to 'char [16] [16]'. The match between vectors and pointers only applies to vectors of a dimension (and look there).

One way that works to solve this problem is to have its function get a reference to the array to be filled and put the values read inside it. I also took the function to get a FILE* function instead of a filename, which is something more generic (for example, you can now read the stdin array instead of a named file)

#define NLIN 16
#define NCOL 16

void lerMatriz(FILE *arquivo, char out[NLIN][NCOL]){
    int c;
    for(int i=0; i<NLIN; i++){
        for(int j=0; j<NCOL; j++){
            c = fgetc(arquivo);
            if(c == EOF){ /*ERRO*/ }
            out[i][j] = c;
        }
        c = fgetc(arquivo);
        if(c != '\n'){ /*ERRO*/ }
    }
}

int main(){
    FILE *entrada = fopen("entrada.txt", "r");

    char tabuleiro[NLIN][NCOL];
    letMatriz(entrada, tabuleiro);


    for(int i = 0; i < NLIN; i ++){
        for(int j = 0; j < NCOL; j++){
            printf("%c", tabuleiro[i][j]);
        }
        printf("\n");
    }

    fclose(entrada);
    return 0;
}

The best way to treat reading errors is as an exercise:)

    
21.06.2015 / 05:26