Advertisement
STANAANDREY

5.6 tp

Mar 27th, 2023 (edited)
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #define ARG_ERROR "WRONG NR. OF ARGS!\n"
  5. #define EVER ;;
  6.  
  7. FILE *openFile(const char path[], const char mode[]) {
  8.   FILE *file = fopen(path, mode);
  9.   if (file == NULL) {
  10.     perror("");
  11.     exit(EXIT_FAILURE);
  12.   }
  13.   return file;
  14. }
  15.  
  16. void closeFile(FILE *file) {
  17.   if (fclose(file) == EOF) {
  18.     perror("");
  19.   }
  20. }
  21.  
  22. int main(int argc, char *argv[]) {
  23.   if (argc < 3) {
  24.     fprintf(stderr, ARG_ERROR);
  25.     exit(-1);
  26.   }
  27.  
  28.   FILE *file1 = openFile(argv[1], "rb");
  29.   FILE *file2 = openFile(argv[2], "rb");
  30.  
  31.   uint8_t byte1, byte2;
  32.   for (EVER) {
  33.     fread(&byte1, sizeof(uint8_t), 1, file1);
  34.     fread(&byte2, sizeof(uint8_t), 1, file2);
  35.     if (byte1 != byte2) {
  36.       const long offset = ftell(file1);
  37.       printf("%08ld: %02x %02x\n", offset, byte1, byte2);
  38.     }
  39.     if (byte1 == 0xA) {
  40.       fread(&byte1, sizeof(uint8_t), 1, file1);
  41.       break;
  42.     }
  43.     if (byte2 == 0xA) {
  44.       fread(&byte2, sizeof(uint8_t), 1, file2);
  45.       break;
  46.     }
  47.   }
  48.  
  49.   while (fread(&byte1, sizeof(uint8_t), 1, file1)) {
  50.     const long offset = ftell(file1);
  51.     printf("%08ld: %02x   \n", offset, byte1);
  52.   }
  53.   while (fread(&byte2, sizeof(uint8_t), 1, file2)) {
  54.     const long offset = ftell(file2);
  55.     printf("%08ld:    %02x\n", offset, byte2);
  56.   }//*/
  57.  
  58.   closeFile(file1);
  59.   closeFile(file2);
  60.   return 0;
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement