Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <cs50.h>
  4. #include "bmp.h"
  5.  
  6. #define SIZE 5
  7.  
  8. int main (int argc, char *argv[])
  9. {
  10.     printf("Hello, World\n");
  11.     FILE *file_input = fopen(argv[1], "r");
  12.     if (file_input == NULL)
  13.     {
  14.         printf("Cannot open file\n");
  15.         return 1;
  16.     }
  17.     BYTE buffer[SIZE];
  18.     int count = 0;
  19.     int n = 0;
  20.     char file_name[4];
  21.     sprintf(file_name, "%03i.txt", n);
  22.     FILE *text_file = fopen(file_name, "w");
  23.     while(fread(buffer, sizeof(buffer), 1, file_input) == 1)
  24.     {
  25.         count++;
  26.         printf("The first char is %#02x and second char is %#02x\n",buffer[0],buffer[1]);
  27.         if (buffer[0] == 0x68 && buffer[1] == 0x2c)
  28.         {
  29.             printf("True\n");
  30.             if (n == 0)
  31.             {
  32.                 fwrite(buffer, sizeof(buffer), 1, text_file);
  33.                 n++;
  34.             } // end of if
  35.             if (n != 0)
  36.             {
  37.                 fclose(text_file);
  38.                 sprintf(file_name, "%03i.txt", n);
  39.                 text_file = fopen(file_name, "w");
  40.                 n++;
  41.             }
  42.             fwrite(buffer, sizeof(buffer), 1, text_file);
  43.         }
  44.         if (n > 0)
  45.         {
  46.             fwrite(buffer, sizeof(buffer), 1, text_file);
  47.         }
  48.  
  49.     } // End of card reading
  50.     int size_buffer = sizeof(buffer);
  51.     printf("The size of buffer is %i\n",size_buffer);
  52.     printf("The number of loop run is %i\n",count);
  53.     fclose(text_file);
  54.     fclose(file_input);
  55. } // End of main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement