I am working with a multi-thread program.
First I redirect my stdout to a certain file. No problem there (I used dup2(fd, 1)
where fd
is the file descriptor for the file).
Afterwards, I need to redirect my stdout to the terminal again.
My first approach:
/*Declaration*/
fpost_t stream_sdout;
/*code*/
if ( fgetpos( stdout, &stream_sdout) == -1 )
perror(Error:);
It says illegal seek.
No idea why this is happening.
But if I get this to work, then I only need to use fsetpos(stdout, &stream_stdout)
and it should work.
My second idea, was to to copy the stdout using dup2(stdout, 4)
to the file descriptor table, at position 4. But that ain't working either.
How can I switch the standard output back to its original destination (terminal, pipe, file, whatever)?
stdout
around like this is probably a bad idea -- what position exactly should the terminal report infgetpos()
? Why not simply open a file and output to it usingfwrite()
orfprintf()
orwrite()
and output to the terminal via/dev/tty
when you need it?stdout
.