FWRITE function is not writing in binary format in C language

Asked

Viewed 345 times

1

I am not able to write data in binary in 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);}

  • What is the definition of struct registro?

  • This is the record: struct registro
{
 char nome_jogo[40];
 int ano_lancamento;
 char genero[20];
 char plataforma[20];
 char status;
 } reg[50];

2 answers

3


Your file has characters for the following reason.

As his struct consists of 4 fields char and a field int, the normal is that the data can be read.

The method fwrite writes directly the values of your struct file, without converting values such as the printf.

The values used are those in the table ascii. Assuming you have the following string "ABCD", the values would be written as: 65,66,67,68. And when the text editor would read these values it would display ABCD.

Case a int be written in the file, the size of the int in 64bits vale 4, and the value to be written will be 65. The value written in the file will be 65,0,0,0 or To to the text editor. If the written value is 16961, shall write AB.

If you use the command string (present in the gcc/ bin) it will be possible to see the names of the methods, the strings and the libraries used by your system, because of the interpretation of the table ascii when you read the file.

If you want a file with unreadable values (no characters A-Za-z-0-9), you must store values that escape as much as possible from literary and numerical characters.

  • I can understand now. I was thinking that the characters would be replaced by symbols as well as it is possible to notice in some binary files since I had no prior knowledge about ASCII. Thank you very much for the explanation!

1

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

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

How would you like the next entry to be presented "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 in the handling of "\n". A binary file does not handle this character, a text file can convert to "\r\n". For more details read the sections 7,21,2 and 7.21.3 of Standard C11 (in English).

  • You mean it is only possible to write in binary if the objects are converted to integer? In the file(file) "Games.dat" only the release year was recorded in binary. I didn’t quite understand the difference between your code and mine. I could explain?

  • 1

    When saving the character 'A' in a (binary) file what is there is the value 65 (on ASCII-based computers). This value when interpreted as a character is displayed with "A". That is, there is no difference between text character and binary character.

  • So if there is no difference, what would be the reason for the file to save characters?

  • The difference between a text or binary file is basically the treatment of "n'". A binary file does not handle this character, a text file can convert to "\r\n". For more details read the sections 7,21,2 and 7.21.3 of Standard C11 (in English).

  • @pmg: what is in your comments should be in the answer - that which is his doubt, and what is important to document here. Could you edit it and pass this information to the answer?

Browser other questions tagged

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