Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #define WRITE 1
- #define READ 0
- #define SIZE 1024
- char *readFile(char *path)
- {
- FILE *file = fopen(path, "r");
- if (!file)
- return NULL;
- fseek(file, 0, SEEK_END);
- int length = ftell(file);
- rewind(file);
- char *result = (char *)calloc(length, sizeof(char));
- int i = 0;
- while (!feof(file))
- result[i++] = fgetc(file);
- fclose(file);
- return result;
- }
- int main()
- {
- char path[50];
- int pdes[2];
- char bufor[SIZE];
- printf("Podaj plik: ");
- scanf("%s", path);
- pipe(pdes);
- printf("PID proc macierzysty: %d\n", getpid());
- if (fork())
- {
- close(pdes[READ]);
- printf("PID proc 1: %d\n", getpid());
- char *content = readFile(path);
- if (!content)
- {
- printf("Nie ma takiego pliku!\n");
- exit(1);
- }
- printf("Przesylam dane\n");
- fflush(stdout);
- sprintf(bufor, "%s", content);
- write(pdes[WRITE], bufor, sizeof(bufor));
- return 0;
- }
- else
- {
- close(pdes[WRITE]);
- printf("PID proc 2: %d\n", getpid());
- fflush(stdout);
- printf("oczekuje na dane\n");
- read(pdes[READ], bufor, sizeof(bufor));
- printf("odebralem dane!\n");
- int n = 0;
- FILE *file = fopen("wynik.txt", "w");
- while (bufor[n])
- {
- if (bufor[n] == -123)
- bufor[n] = 'a';
- else if (bufor[n] == -121)
- bufor[n] = 'c';
- else if (bufor[n] == -103)
- bufor[n] = 'e';
- else if (bufor[n] == -124)
- bufor[n] = 'n';
- else if (bufor[n] == -126)
- bufor[n] = 'l';
- else if (bufor[n] == -77)
- bufor[n] = 'o';
- else if (bufor[n] == -101)
- bufor[n] = 's';
- else if (bufor[n] == -70)
- bufor[n] = 'z';
- else if (bufor[n] == -68)
- bufor[n] = 'z';
- if (!(bufor[n] == -59 || bufor[n] == -60 || bufor[n] == -61 || bufor[n] == -1))
- fprintf(file, "%c", bufor[n]);
- n++;
- }
- fclose(file);
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement