Passing information from a file to an array

Asked

Viewed 3,569 times

2

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

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 that 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 matrix 16 x 16, char* arquivo my string containing the file name and int l and int c the number of rows and columns of the matrix (16 x 16).

What would be the problem in my code?

1 answer

0


There are several problems with your code. I’ll tell you what I found but I can’t guarantee it’s an exhaustive list.

First of all don’t just read 256 characters of input and play straight into the matrix. You’re forgetting the line breaks (" n")

Second, l and c Is it always 16, or could it be other numbers too? If it’s always 16 you don’t have to pass them as a parameter and if it’s not always 16 you should be using these 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 it’s always 16, because then we can use static allocation, which simplifies things.

A third problem is that it is not clear what its function does, that is: what it receives and what it returns. What is the difference between board and plank P? Why did you say that the function returns a char** but does not use any return?

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

One way that works to solve this problem is to make your function receive a reference to the matrix to be filled and put the values read inside it. I also used to make the function receive a FILE* 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 exercise :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.