SHARE
TWEET

Untitled

razvanth21 Nov 23rd, 2018 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7.  
  8. int main(void)
  9. {
  10.     int filedes[2];
  11.     pid_t pid;
  12.     char c;
  13.     int status;
  14.     int r, i, k = 0;
  15.     char buf[32];
  16.  
  17.     if (pipe(filedes) < 0)
  18.     {
  19.         fprintf(stderr, "Eroare la crearea pipe-ului.\n");
  20.         exit(EXIT_FAILURE);
  21.     }
  22.  
  23.     if ((pid = fork()) < 0)
  24.     {
  25.         fprintf(stderr, "Eroare la crearea copilului.\n");
  26.         exit(EXIT_FAILURE);
  27.     }
  28.  
  29.     if (pid == 0)
  30.     {
  31.         if (close(filedes[0]) == -1)
  32.             exit(EXIT_FAILURE);
  33.    
  34.         int fd;
  35.  
  36.         if ((fd = open("file.txt", O_RDONLY)) == -1)
  37.             exit(EXIT_FAILURE);
  38.  
  39.         while ((r = read(fd, buf, sizeof (buf))))
  40.             write(filedes[1], buf, r);
  41.  
  42.         if (close(fd) == -1)
  43.             exit(EXIT_FAILURE);
  44.  
  45.         if (close(filedes[1]) == -1)
  46.             exit(EXIT_FAILURE);
  47.  
  48.         exit(0);
  49.     }
  50.     else
  51.     {
  52.         if (close(filedes[1]) == -1)
  53.             exit(EXIT_FAILURE);
  54.  
  55.         while ((r = read(filedes[0], buf, sizeof (buf))))
  56.         {
  57.             for (i = 0; i < r; i++)
  58.                 if (buf[i] >= '0' && buf[i] <= '9')
  59.                     k ++;
  60.         }
  61.        
  62.         printf("%d\n", k);
  63.  
  64.         if (close(filedes[0]) == -1)
  65.             exit(EXIT_FAILURE);
  66.  
  67.         exit(0);
  68.     }
  69.  
  70.     wait(&status);
  71.     return 0;
  72. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top