28

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...

Alexis Wilke
  • 17,282
  • 10
  • 73
  • 131
poy
  • 9,640
  • 9
  • 43
  • 71

2 Answers2

45

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.)

Fred Foo
  • 342,876
  • 71
  • 713
  • 819
3

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

popen("nedit 2>/dev/null", "r");
qbert220
  • 10,780
  • 3
  • 29
  • 31