How to view contents of a file . bin generated in c

Asked

Viewed 2,223 times

1

I had to create a C program that reads a motion sensor activation file (.txt) and produces a binary file with the same information.

The file . txt has the following information:

   3B2 20051023 014857393 609f
   3B3 20051023 014857393 00ff
   3B4 20051023 014857503 609f
   3B5 20051023 014857503 00ff
   3B6 20051023 014857613 6093
   3B7 20051023 014857613 807f
   3B8 20051023 014857723 609f
   3B9 20051023 014857723 00ff
   3BA 20051023 014857834 609f
   3BB 20051023 014857864 00ff
   3BC 20051023 014904113 807f
   3BD 20051023 014904113 08f7
   3BE 20051023 014904223 807f
   3BF 20051023 014904223 00f7

I created the following code in C to read the file and save it inside a new file. bin, my doubt is on how to open that file. bin and check if what is saved is correct, there is some program that converts . bin file to . txt?

Code:

#include <stdio.h>

int main() {
    int  data1,hora1;
    char sequencial1[4],ativacao1[5],nomeEntrada[50],nomeSaida[50];
    FILE *arqEntrada, *arqSaida;

    printf("Digite 2 nomes de arquivo: ");
    scanf("%s %s",&nomeEntrada,&nomeSaida);

    if ((arqEntrada = fopen(nomeEntrada,"r")) == NULL) {
        printf("Problema na abertura do arquivo %s.\n",nomeEntrada);
        return -1;      
    }
    if ((arqSaida = fopen(nomeSaida,"wb")) == NULL) {
        printf("Problema na abertura do arquivo %s.\n",nomeSaida);
        return -1;      
    }
    while (fscanf(arqEntrada,"%s %d %d %s",&sequencial1, &data1, &hora1, &ativacao1) > 0) {
            fwrite(&sequencial1,sizeof(char),3,arqSaida);
            fwrite(&data1,sizeof(int),1,arqSaida);
            fwrite(&hora1,sizeof(int),1,arqSaida);
            fwrite(&ativacao1,sizeof(char),4,arqSaida);
    } 

    fclose(arqEntrada);
    fclose(arqSaida);

    return 0;
}
  • Windows or Linux?

  • I’m using the windows.

  • 2

    Here has a binary file editor/viewer for Windows.

  • 2

    Or this that I have personally used

1 answer

2


Converter:

#include <stdio.h>

#define MAX_BUF_LEN 100

int main( int argc, char * argv[] )
{
    FILE * fin = NULL;
    FILE * fout = NULL;
    char buf[ MAX_BUF_LEN ] = {0};
    int nlinha = 1;
    int n = 0;
    int seq = 0;
    int ativ = 0;
    int dt = 0L;
    int hr = 0L;

    if(argc < 3)
    {
        printf( "Erro de Sintaxe:\n\t%s entrada.txt saida.bin\n", argv[0] );
        return 1;
    }

    if((fin = fopen( argv[1] , "r")) == NULL)
    {
        printf( "Erro abrindo arquivo para leitura: %s\n", argv[1] );
        return 1;
    }

    if((fout = fopen(argv[2], "wb")) == NULL)
    {
        printf( "Erro abrindo arquivo para gravacao: %s\n", argv[2] );
        fclose(fin);
        return 1;
    }

    while( fgets( buf, MAX_BUF_LEN, fin) )
    {

        if((n = sscanf( buf, "%X %d %d %x", &seq, &dt, &hr, &ativ )) != 4)
        {
            printf( "Erro de leitura: %s:%d\n", argv[1], nlinha );
            continue;
        }

        fwrite( &seq, sizeof(short), 1, fout );
        fwrite( &dt, sizeof(int), 1, fout );
        fwrite( &hr, sizeof(int), 1, fout );
        fwrite( &ativ, sizeof(short), 1, fout );

        nlinha++;
    }

    fclose(fout);
    fclose(fin);

    return 0;
}

Entree:

   3B2 20051023 014857393 609f
   3B3 20051023 014857393 00ff
   3B4 20051023 014857503 609f
   3B5 20051023 014857503 00ff
   3B6 20051023 014857613 6093
   3B7 20051023 014857613 807f
   3B8 20051023 014857723 609f
   3B9 20051023 014857723 00ff
   3BA 20051023 014857834 609f
   3BB 20051023 014857864 00ff
   3BC 20051023 014904113 807f
   3BD 20051023 014904113 08f7
   3BE 20051023 014904223 807f
   3BF 20051023 014904223 00f7

Exit:

$ xxd -g1 -c12 teste.bin 
0000000: b2 03 4f f4 31 01 b1 b4 e2 00 9f 60  ..O.1......`
000000c: b3 03 4f f4 31 01 b1 b4 e2 00 ff 00  ..O.1.......
0000018: b4 03 4f f4 31 01 1f b5 e2 00 9f 60  ..O.1......`
0000024: b5 03 4f f4 31 01 1f b5 e2 00 ff 00  ..O.1.......
0000030: b6 03 4f f4 31 01 8d b5 e2 00 93 60  ..O.1......`
000003c: b7 03 4f f4 31 01 8d b5 e2 00 7f 80  ..O.1.......
0000048: b8 03 4f f4 31 01 fb b5 e2 00 9f 60  ..O.1......`
0000054: b9 03 4f f4 31 01 fb b5 e2 00 ff 00  ..O.1.......
0000060: ba 03 4f f4 31 01 6a b6 e2 00 9f 60  ..O.1.j....`
000006c: bb 03 4f f4 31 01 88 b6 e2 00 ff 00  ..O.1.......
0000078: bc 03 4f f4 31 01 31 6b e3 00 7f 80  ..O.1.1k....
0000084: bd 03 4f f4 31 01 31 6b e3 00 f7 08  ..O.1.1k....
0000090: be 03 4f f4 31 01 9f 6b e3 00 7f 80  ..O.1..k....
000009c: bf 03 4f f4 31 01 9f 6b e3 00 f7 00  ..O.1..k....
  • You’ve been a great help, thank you.

Browser other questions tagged

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