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?
#define WRITE 1
#define READ 0
using namespace std;
int fd_in, fd_out,
if(fork() == 0)
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, "c\n", 2);
write(fd_out, "a\n", 2);
write(fd_out, "b\n", 2);
while((c = read(fd_in, buf, 5)) > 0)
write(1, buf, c);