Advertisement
AdrianMadajewski

Untitled

May 18th, 2020
3,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5. int countBinary(unsigned int value) {
  6.     unsigned int count = 0;
  7.     while (value) {
  8.         count += value & 1;
  9.         value >>= 1;
  10.     }
  11.     return count;
  12. }
  13.  
  14. int main(int argc, char* argv[])
  15. {
  16.     const int WA = 11;
  17.     FILE* data;
  18.     FILE* alfa;
  19.     FILE* beta;
  20.  
  21.     errno_t errd;
  22.     errno_t erra;
  23.     errno_t errb;
  24.  
  25.     errd = fopen_s(&data, "Odczyty.txt", "r");
  26.     erra = fopen_s(&alfa, "Alfa.txt", "w");
  27.     errb = fopen_s(&beta, "Beta.txt", "w");
  28.  
  29.     if (errd != 0 || erra != 0 || errb != 0) {
  30.         printf("Error openning files.\n");
  31.         return 1;
  32.     }
  33.  
  34.     // Variable for chaning the output
  35.     FILE* current = alfa;
  36.    
  37.     unsigned int number;
  38.    
  39.     while (fscanf_s(data, "%u", &number) == 1) {
  40.         if (countBinary(number) == WA && current == alfa) {
  41.             current = beta;
  42.             continue;
  43.         }
  44.         else if (countBinary(number) == WA && current == beta) {
  45.             current = alfa;
  46.             continue;
  47.         }
  48.        
  49.         fprintf(current, "%u\n", number);
  50.     }
  51.     if (feof(data)) {
  52.         printf("EOF - Procces finished.\n");
  53.     }
  54.     else {
  55.         printf("EOF error.\n");
  56.         return 1;
  57.     }
  58.  
  59.     fclose(data);
  60.     fclose(alfa);
  61.     fclose(beta);
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement