Function FWRITE is not writing in binary format in C language

1

I am not able to write data in binary to a file using the C language. Even using 'wb' the output of the file are characters. How can I proceed? Here's part of my code:

void cadastrar(void)
{
     if((fp=fopen("Jogos.dat", "wb"))==NULL)
     {
        printf("\nO arquivo nao pode ser aberto!\n");
        getch();
        exit(1);
     }
     fseek(fp,0L, SEEK_END);

 do
 {
    printf("\n Digite o Nome do Jogo ('FIM' para sair): ");
    gets(reg[quantidade_cadastro].nome_jogo);

    if ((strcmp(reg[quantidade_cadastro].nome_jogo,"fim")!=0)&&(strcmp(reg[quantidade_cadastro].nome_jogo,"FIM")!=0))
    {
        printf("\n Ano de Lancamento: ");
        scanf("%d",&reg[quantidade_cadastro].ano_lancamento);
        fflush(stdin);

        printf("\n Genero: ");
        gets(reg[quantidade_cadastro].genero);

        printf("\n Plataforma: ");
        gets(reg[quantidade_cadastro].plataforma);

        reg[0].status='1';

        if(fwrite(&reg, sizeof(struct registro), 1, fp) != 1)
        {
            printf("\nErro de Gravacao :/");
            getch();
        }
        else
        {
            quantidade_cadastro++;
            printf("\n Jogo Gravado no Arquivo!\n\n");
        }
    }
 }while((strcmp(reg[quantidade_cadastro].nome_jogo,"fim")!=0)&&(strcmp(reg[quantidade_cadastro].nome_jogo,"FIM")!=0));
 fclose(fp);}

    
asked by anonymous 18.05.2016 / 14:01

2 answers

2

Your file has characters for the following reason.

Since your struct is composed of 4 fields char and a int field, the normal data can be read.

The fwrite method directly writes the values of its struct within the file, without doing a conversion of values like printf .

The values used are those of the ascii table. Assuming you have the following% w / o% "ABCD", the values would be written as: 65,66,67,68 . And when the text editor were to read those values it would display ABCD .

If a string is written to the file, the size of int in 64bits is 4 , and the value to be written is 65. The written value in the file will be 65,0,0,0 or A for the text editor. If the written value is 16961 , you should type AB .

If you use the int command (present in the gcc bin) you can see the method names, string , and libraries used by your system, because of the ascii when reading the file.

If you want a file with unreadable values (without A-Za-z-0-9 characters), you should store values that flee as far as possible from literary and numeric characters.     

18.05.2016 / 19:15
1

The content of objects of type struct registro is basically text only. No wonder the contents of the file are characters.

Check the ano_lancamento part of the "Games.dat" file.

How would you like the next record to appear "in binary"?

strcpy(reg[0].nome_jogo, "jogo um");
reg[0].ano_lancamento = 2014;
strcpy(reg[0].genero, "sorte");
strcpy(reg[0].plataforma, "papel e lápis");

The difference between a text or binary file is basically the "\n" processing. A binary file does not handle this character, a text file can convert to "\r\n" . For more details read the Standard C11 Sections 7.21,2 and 7.21.3 ) .

    
18.05.2016 / 14:15