Hey,
I'm trying to redirect stdout, so it goes into a pipe, so I can do something like this:
printf("Hey there!\n");
then read from the pipe and get "Hey there!".
The code I'm trying to use for this is the following:
Code:
// Redirect output to file
int pipePair[2];
if(pipe(pipePair) != 0) {
fprintf(stderr,"Could not create pipe. Skipping command\n");
exit(1);
}
if (dup2(pipePair[1], STDOUT_FILENO) == -1) {
fprintf(stderr, "Error\n");
exit(1);
}
char buffer[100];
memset(buffer,0,100);
printf("Hey there!\n");
read(pipePair[0],buffer,3); // Read only the first 3 letters just in case
printf("%s\n", buffer);
Now, this code don't return.
read() blocks, because for some reason.
If I write manually to the pipe by saying write(pipePair[1]......blabla),
it don't block, but it never recieves anything from stdout, so it seems the redirection failed.
Anyone see what I do wrong?