I'm trying to use pipes to write some data on a program stdin and get the stdout.
I found this one on the internet and changed it, (the original didn't seems to work to):
I need to write some data on the sort's stdin and get the stdout, someone can help me?Code:#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <iostream> #define WRITE 1 #define READ 0 using namespace std; int main() { char buf[5]; int fd_in[2], fd_out[2], c; pipe(fd_in);pipe(fd_out); if(fork() == 0) { close(fd_in[WRITE]);dup2(fd_in[READ], READ); close(fd_out[READ]); dup2(fd_out[WRITE], WRITE); dup2(fd_out[WRITE], WRITE); //stdout -> pipe's write (everything what would go to stdout will go to the pipe's write... execlp("sort", "sort", NULL); } write(fd_out[1], "c\n", 2); write(fd_out[1], "a\n", 2); write(fd_out[1], "b\n", 2); while((c = read(fd_in[0], buf, 5)) > 0) write(1, buf, c); return 0; }



LinkBack URL
About LinkBacks



