Guest User

Untitled

a guest
Sep 2nd, 2020
105
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char* argv[])
  5. {
  6.     if (argc != 2)
  7.     {
  8.         printf("Usage: %s <filename>\n", argv[0]);
  9.         exit(EXIT_FAILURE);
  10.     }
  11.  
  12.     FILE* fp;
  13.     int ch = '\0';
  14.     int count = 0;
  15.     char cmp = 0x1a;
  16.     if ((fp = fopen(argv[1], "rb")) == NULL)
  17.     {
  18.         printf("Error reading %s", argv[1]);
  19.         exit(EXIT_FAILURE);
  20.     }
  21.  
  22.     while (!feof(fp))
  23.     {
  24.         ch = fgetc(fp);
  25.         if (!feof(fp) && ch == EOF) // **
  26.         {
  27.             puts("Terminating program: Error reading from file");
  28.             exit(EXIT_FAILURE);
  29.         }
  30.         if (ch == cmp) count += 1;
  31.     }
  32.  
  33.     fclose(fp);
  34.  
  35.     printf("Number of entries: %d\n", count);
  36.     return 0;
  37. }
  38.  
RAW Paste Data