Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3.  
  4. int main()
  5. {
  6.     int fd1[2];
  7.     int lacze1=pipe(fd1);
  8.     int fd2[2];
  9.     int lacze2=pipe(fd2);
  10.  
  11.     int otwierany;
  12.     char blad[]= "Blad";
  13.     char nazwapliku[20];
  14.  
  15.     char nazwa[20];
  16.     printf("podaj nazwe pliku \n");  
  17.     scanf("%s", nazwa);
  18.     char zawartosc[8];
  19.  
  20.     int potomny=fork();
  21.     switch(potomny)
  22.    {
  23.     case 0:
  24.  
  25.         read(fd1[0],nazwapliku,10);
  26.     close(fd1[0]);
  27.         otwierany=open(nazwa,"O_RDONLY");
  28.  
  29.         if(otwierany==-1)
  30.         { write(fd2[1], blad,6);}
  31.  
  32.         else {
  33.  
  34.     char bufor[1];
  35.         int bajt;
  36.         bajt=read(otwierany,bufor, 1);
  37.         write(fd2[1],bufor,1);
  38.         while(bajt>0)
  39.     {
  40.         bajt=read(otwierany,bufor, 1);
  41.         write(fd2[1],bufor,1);
  42.     }
  43.     close(fd2[1]);
  44.     }
  45.  
  46.     break;
  47.  
  48.  
  49.         default:
  50.  
  51.         write(fd1[1],nazwa, 10);
  52.         close(fd1[1]);
  53.         wait();
  54.         read(fd2[0], zawartosc , 8);
  55.         printf("%s \n ", zawartosc);
  56.     break;
  57.  
  58.    }
  59.  
  60.  
  61.  
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement