Read char matrix from a binary file

Asked

Viewed 156 times

0

Function that writes and reads binary file:

int FUNCAO_QUE_GRAVA_BIN (char filename[],int partida1,char resultado1)
{
        typedef struct  {
                int partida;
                char jogvelha[3][3];
                char resultado;
        } velha;

        velha partida = {partida1,{"a","b"},resultado1},read_data;

        FILE * file= fopen(filename, "wb");
        if (file != NULL) {
                fwrite(&partida, sizeof(velha), 1, file);
                fclose(file);

                FILE* fin = fopen(filename, "rb");
                fread(&read_data, sizeof(velha), 1, file);
                printf("%d %c %c\n", read_data.partida, read_data.jogvelha[3][3], read_data.
                       resultado);
                fclose(fin);
                fflush(stdin);
                while(getchar()!='\n'); // option TWO to clean stdin
                getchar(); // wait for ENTER
                return 0;//failure //ignore
        }

        return 1; //sucesso /ignore
}

calling on the main :

        int ganhador=1;
        char local[]={"binar"};
        int partidas= 1;

  FUNCAO_QUE_GRAVA_BIN(local,partidas,ganhador);

the problem is that the output is going all wrong, the game[3][3] does not come out right, as tidy?

  • If jogvelha is an array of 3 by 3, so read_data.jogvelha[3][3] is wrong. An array of size 3 has boxes 0, 1 and 2

  • @Isac on the printf you say?

  • Yes in the printf

  • @Isac printf("%d %c %c\n", read_data.partida, read_data.jogvelha[0][1], read_data.
 resultado); output: 1, letra do ASCII not only is the output of the read_data.resultado

1 answer

2

The writing and reading in file you have is correct, the problem are some errors and your misconceptions in the code:

  • As I said in comment printf is wrong to print a house that doesn’t exist:

    printf("%d %c %c\n", read_data.partida, read_data.jogvelha[3][3], read_data.resultado);
    //                                                         ^--^
    

    If you have a 3 by 3 board, then you have boxes 0, 1 and 2. Box 3 is already out. It is important to remember that the first house of an array is always 0.

  • Defined the resultado as char in the structure:

    typedef struct  {
        ...
        char resultado;
    } velha;
    

    But then save the numerical value 1 in this field:

    int ganhador=1;
    FUNCAO_QUE_GRAVA_BIN(local,partidas,ganhador);
    //                                     ^-- aqui passa um int com 1 em vez de um char
    

    This will make you see such a strange character of the ASCII table, more specifically the ASCII character 1.

  • The initialization of jogvelha is also wrong:

    velha partida = {partida1, {"a","b"},resultado1}
    //                         ^^^^^^^^
    

    If you have an array of chars 3 by 3 then there are characters missing, as well as a string missing. Another detail you forgot is that if you save the values as strings then the compiler includes the terminator, which makes it less space, and you need to have a 4 by 4 board.

    Better to start as char whether or not by writing the characters you want manually:

    velha partida = {partida1, {{'X','O','X'}, {'X','O','X'}, {'X','O','X'}}, resultado1};
    

Browser other questions tagged

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