Advertisement
STANAANDREY

mcview sim

Mar 21st, 2023 (edited)
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #define NR 16
  5.  
  6. int main(int argc, char **argv) {
  7.   if (argc < 2) {
  8.     fprintf(stderr, "args error!\n");
  9.     exit(-1);
  10.   }
  11.  
  12.   FILE *file = fopen(argv[1], "rb");
  13.   if (file == NULL) {
  14.     perror("opening error");
  15.     exit(-1);
  16.   }
  17.  
  18.   while (!feof(file)) {
  19.     uint8_t buffer[NR] = {};
  20.     printf("%08lX: ", ftell(file));
  21.     fread(buffer, sizeof(*buffer), NR, file);
  22.     for (int i = 0; i < NR; i++) {
  23.       printf("%02X ", buffer[i]);
  24.     }
  25.  
  26.     putchar('|');
  27.     for (int i = 0; i < NR; i++) {
  28.       if (buffer[i] >= 32) {
  29.         putchar(buffer[i]);
  30.       } else {
  31.         putchar('.');
  32.       }
  33.     }
  34.     puts("|");
  35.   }
  36.  
  37.   if (fclose(file) == EOF) {
  38.     perror(NULL);
  39.   }
  40.   return 0;
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement