I had to create a program in C that reads a motion sensor activation file (.txt) and produces a binary file with the same information.
The .txt file 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 .bin file, my doubt is in how to open this .bin file and check if what is written is correct, there is some program that converts .bin file for .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;
}