Advertisement
arczi316

Pipe_SO_lab9_Y5

Dec 8th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4.  
  5. #define WRITE 1
  6. #define READ 0
  7. #define SIZE 1024
  8.  
  9. char *readFile(char *path)
  10. {
  11.     FILE *file = fopen(path, "r");
  12.  
  13.     if (!file)
  14.         return NULL;
  15.  
  16.     fseek(file, 0, SEEK_END);
  17.     int length = ftell(file);
  18.     rewind(file);
  19.  
  20.     char *result = (char *)calloc(length, sizeof(char));
  21.     int i = 0;
  22.  
  23.     while (!feof(file))
  24.         result[i++] = fgetc(file);
  25.  
  26.     fclose(file);
  27.     return result;
  28. }
  29.  
  30. int main()
  31. {
  32.     char path[50];
  33.     int pdes[2];
  34.     char bufor[SIZE];
  35.  
  36.     printf("Podaj plik: ");
  37.     scanf("%s", path);
  38.  
  39.     pipe(pdes);
  40.  
  41.     printf("PID proc macierzysty: %d\n", getpid());
  42.  
  43.     if (fork())
  44.     {
  45.         close(pdes[READ]);
  46.         printf("PID proc 1: %d\n", getpid());
  47.         char *content = readFile(path);
  48.  
  49.         if (!content)
  50.         {
  51.             printf("Nie ma takiego pliku!\n");
  52.             exit(1);
  53.         }
  54.  
  55.         printf("Przesylam dane\n");
  56.  
  57.         fflush(stdout);
  58.         sprintf(bufor, "%s", content);
  59.         write(pdes[WRITE], bufor, sizeof(bufor));
  60.  
  61.         return 0;
  62.     }
  63.     else
  64.     {
  65.         close(pdes[WRITE]);
  66.  
  67.         printf("PID proc 2: %d\n", getpid());
  68.         fflush(stdout);
  69.         printf("oczekuje na dane\n");
  70.         read(pdes[READ], bufor, sizeof(bufor));
  71.         printf("odebralem dane!\n");
  72.  
  73.         int n = 0;
  74.         FILE *file = fopen("wynik.txt", "w");
  75.         while (bufor[n])
  76.         {
  77.             if (bufor[n] == -123)
  78.                 bufor[n] = 'a';
  79.             else if (bufor[n] == -121)
  80.                 bufor[n] = 'c';
  81.             else if (bufor[n] == -103)
  82.                 bufor[n] = 'e';
  83.             else if (bufor[n] == -124)
  84.                 bufor[n] = 'n';
  85.             else if (bufor[n] == -126)
  86.                 bufor[n] = 'l';
  87.             else if (bufor[n] == -77)
  88.                 bufor[n] = 'o';
  89.             else if (bufor[n] == -101)
  90.                 bufor[n] = 's';
  91.             else if (bufor[n] == -70)
  92.                 bufor[n] = 'z';
  93.             else if (bufor[n] == -68)
  94.                 bufor[n] = 'z';
  95.  
  96.             if (!(bufor[n] == -59 || bufor[n] == -60 || bufor[n] == -61 || bufor[n] == -1))
  97.                 fprintf(file, "%c", bufor[n]);
  98.  
  99.             n++;
  100.         }
  101.         fclose(file);
  102.         return 0;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement