【问题标题】:IO redirect: cout not working in mainIO 重定向:cout 在 main 中不起作用
【发布时间】:2014-05-02 06:13:14
【问题描述】:

我有一个函数可以将标准输出重定向到管道。

我从 main 调用函数,在调用函数之前,“cout”工作正常。在函数之后,cout

我认为函数中的那些 IO 重定向使 cout 在 main 中不起作用。

无论如何我可以在 main 函数之后使用 cout 来输出到屏幕上吗? cout 和 cerr 有什么区别?

谢谢!

【问题讨论】:

  • 您确实需要发布一些代码来显示您在做什么 - 很可能,您正在关闭 stdout 然后将其关闭....

标签: c++ ios linux io pipe


【解决方案1】:

我从另一个问题中找到了解决方案。 这是解决方案:

int o = dup(fileno(stdout));

//call the function that does the IO redirect thing

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

来自Michael Krelin's answer

感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-25
    • 2011-09-08
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    相关资源
    最近更新 更多