C
Christof Warlich
Hi,
besides stdout and stderr, I would like to connect a third
output stream to my terminal, as I want to be able to redirect
stdout, stderr and the third output stream separately. But the
following attempt:
#include <unistd.h>
#include <stdio.h>
int main(void) {
FILE *stdalt = fdopen(dup(fileno(stderr)), "w");
printf("Alternative file descriptor: %d\n", fileno(stdalt));
fprintf(stdout, "STDOUT\n");
fprintf(stderr, "STDERR\n");
fprintf(stdalt, "STDALT\n");
return 0;
}
does not do what I intended: When I run the program, I see that
the newly created descriptor has number 3 as expected:
$ ./a.out
Alternative file descriptor: 3
STDOUT
STDERR
STDALT
but when I redirect stderr, stdalt gets redirected as well:
../a.out 2>/dev/null
Alternative file descriptor: 3
STDOUT
But I'd like to get something like this:
$ ./a.out 2>/dev/null
Alternative file descriptor: 3
STDOUT
STDALT
$ ./a.out 3>/dev/null
Alternative file descriptor: 3
STDOUT
STDERR
Can anyone tell how to do this right?
Thanks,
Christof
besides stdout and stderr, I would like to connect a third
output stream to my terminal, as I want to be able to redirect
stdout, stderr and the third output stream separately. But the
following attempt:
#include <unistd.h>
#include <stdio.h>
int main(void) {
FILE *stdalt = fdopen(dup(fileno(stderr)), "w");
printf("Alternative file descriptor: %d\n", fileno(stdalt));
fprintf(stdout, "STDOUT\n");
fprintf(stderr, "STDERR\n");
fprintf(stdalt, "STDALT\n");
return 0;
}
does not do what I intended: When I run the program, I see that
the newly created descriptor has number 3 as expected:
$ ./a.out
Alternative file descriptor: 3
STDOUT
STDERR
STDALT
but when I redirect stderr, stdalt gets redirected as well:
../a.out 2>/dev/null
Alternative file descriptor: 3
STDOUT
But I'd like to get something like this:
$ ./a.out 2>/dev/null
Alternative file descriptor: 3
STDOUT
STDALT
$ ./a.out 3>/dev/null
Alternative file descriptor: 3
STDOUT
STDERR
Can anyone tell how to do this right?
Thanks,
Christof