Advertisement
STANAANDREY

read int bin

Jan 9th, 2023
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #define INPUT_FILE_NAME "integers.bin"
  5. #define OUTPUT_FILE_NAME "hex.txt"
  6.  
  7. FILE *safeOpenFile(const char *const path, const char *const mode) {
  8.     FILE *file = NULL;
  9.     file = fopen(path, mode);
  10.     if (file == NULL) {
  11.         perror("opening error!");
  12.         exit(EXIT_FAILURE);
  13.     }
  14.     return file;
  15. }
  16.  
  17. void safeCloseFile(FILE *file) {
  18.     if (fclose(file) == EOF) {
  19.         perror(NULL);
  20.     }
  21. }
  22.  
  23. int main(void) {
  24.     FILE *fin = safeOpenFile(INPUT_FILE_NAME, "rb");
  25.     FILE *fout = safeOpenFile(OUTPUT_FILE_NAME, "w");
  26.  
  27.     for (uint16_t aux; fread(&aux, sizeof(aux), 1, fin) == 1;) {
  28.         fprintf(fout, "%08x\n", aux);
  29.     }
  30.  
  31.     safeCloseFile(fin);
  32.     safeCloseFile(fout);
  33.     return 0;
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement