Advertisement
pochti_da

Untitled

Oct 6th, 2020
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6.  
  7. typedef struct Node
  8. {
  9.     int32_t key;
  10.     int32_t left_ind;
  11.     int32_t right_ind;
  12. } Node;
  13.  
  14. int
  15. read_node(int fd, int n, Node *nd)
  16. {
  17.     return ((lseek(fd, sizeof(Node) * n, SEEK_SET) != -1) &&
  18.             (read(fd, nd, sizeof(*nd)) == sizeof(*nd)));
  19. }
  20.  
  21. int
  22. rec(int fd, int n)
  23. {
  24.     Node nd;
  25.     if (!read_node(fd, n, &nd)) {
  26.         return 0;
  27.     }
  28.  
  29.     if (nd.right_ind != 0) {
  30.         rec(fd, nd.right_ind);
  31.     }
  32.  
  33.     printf("%d\n", nd.key);
  34.  
  35.     if (nd.left_ind != 0) {
  36.         rec(fd, nd.left_ind);
  37.     }
  38.  
  39.     return 1;
  40. }
  41.  
  42. int
  43. main(int argc, char *argv[])
  44. {
  45.     int fd = -1;
  46.     if ((fd = open(argv[1], O_RDONLY)) == -1 ||
  47.             !rec(fd, 0)) {
  48.         fprintf(stderr, "Error error\n");
  49.         return 1;
  50.     }
  51.  
  52.     close(fd);
  53.     return 0;
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement