c popen won't catch stderr

送分小仙女□ 提交于 2019-11-26 04:53:45

问题


I\'m trying to use popen() to catch the stderr of a call, but of course it doesn\'t seem to be doing that. Any ideas?

My code looks more or less like this:

popen(\"nedit\", \"r\");

But I\'m getting all this garbage about non-utf8 on my screen...


回答1:


popen gives you a file handle on a process' stdout, not its stderr. Its first argument is interpreted as a shell command, so you can do redirections in it:

FILE *p = popen("prog 2>&1", "r");

or, if you don't want the stdout at all,

FILE *p = popen("prog 2>&1 >/dev/null", "r");

(Any other file besides /dev/null is acceptable as well.)




回答2:


If you want to discard all of the error messages, then you can use:

popen("nedit 2>/dev/null", "r");


来源:https://stackoverflow.com/questions/6900577/c-popen-wont-catch-stderr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!