Advertisement
pochti_da

Untitled

Oct 4th, 2020
1,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6.  
  7. enum
  8. {
  9.     BUFF_SIZE = 4,
  10.     CHAR_SIZE = 8
  11. };
  12.  
  13. int
  14. main(int argc, char *argv[])
  15. {
  16.     unsigned char buff[BUFF_SIZE];
  17.     int fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0600);
  18.  
  19.     int n;
  20.     while (scanf("%d", &n) > 0) {
  21.         buff[sizeof(buff) - 1] = (unsigned char)(n & 0x0000ff);
  22.         buff[sizeof(buff) - 2] = (unsigned char)((n & 0x000f00) >> 8);
  23.         buff[sizeof(buff) - 3] = (unsigned char)((n & 0x0ff000) >> 12);
  24.         buff[sizeof(buff) - 4] = (unsigned char)((n & 0xf00000) >> 20);
  25.  
  26.         if (write(fd, buff, sizeof(buff)) == -1) {
  27.             return 1;
  28.         }
  29.     }
  30.  
  31.     close(fd);
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement