0

I have a function in which standard output is redirected to a pipe.

I call the function from main, before calling the function, "cout" works fine. After the function, cout<< doesn't output anything, nor does write(1,"",..). But cerr could still output to console.

I think are those IO redirect in the function make cout not work in main.

Is there anyway I could use cout in main ,after the function, to out put to the screen? What's the difference between cout and cerr?

THANKS!

fuiiii
  • 1,359
  • 3
  • 17
  • 33
  • You really need to post some code to show what you are doing - most likely, you are closing `stdout` and then leaving it closed.... – Mats Petersson May 02 '14 at 08:17

1 Answers1

0

I found the solution from another question. Here's the solution:

int o = dup(fileno(stdout));

//call the function that does the IO redirect thing

dup2(o,fileno(stdout));
close(o);

It's from Michael Krelin's answer.

Thanks for everyone's help!

Community
  • 1
  • 1
fuiiii
  • 1,359
  • 3
  • 17
  • 33