Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- int main(void)
- {
- int filedes[2];
- pid_t pid;
- char c;
- int status;
- int r, i, k = 0;
- char buf[32];
- if (pipe(filedes) < 0)
- {
- fprintf(stderr, "Eroare la crearea pipe-ului.\n");
- exit(EXIT_FAILURE);
- }
- if ((pid = fork()) < 0)
- {
- fprintf(stderr, "Eroare la crearea copilului.\n");
- exit(EXIT_FAILURE);
- }
- if (pid == 0)
- {
- if (close(filedes[0]) == -1)
- exit(EXIT_FAILURE);
- int fd;
- if ((fd = open("file.txt", O_RDONLY)) == -1)
- exit(EXIT_FAILURE);
- while ((r = read(fd, buf, sizeof (buf))))
- write(filedes[1], buf, r);
- if (close(fd) == -1)
- exit(EXIT_FAILURE);
- if (close(filedes[1]) == -1)
- exit(EXIT_FAILURE);
- exit(0);
- }
- else
- {
- if (close(filedes[1]) == -1)
- exit(EXIT_FAILURE);
- while ((r = read(filedes[0], buf, sizeof (buf))))
- {
- for (i = 0; i < r; i++)
- if (buf[i] >= '0' && buf[i] <= '9')
- k ++;
- }
- printf("%d\n", k);
- if (close(filedes[0]) == -1)
- exit(EXIT_FAILURE);
- exit(0);
- }
- wait(&status);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement