Q:

stderr output when using popen in C++ linux

when I use this simple program to get the standard output of the process, I also somehow get the standard error of the process, although the man page for popen says that only the standard output is redirected. Is it possible that this is related to the shell that is used when forking a process with popen? errEr is a simple program outputting to stderr (cerr << "hello";). I'm using RHEL 6.4. Thanks!
#include <iostream>
#include <cstdio>

using namespace std;

int main ()
{
    FILE *fd = popen("errWr", "r");
    char str [1024];
    while (fgets(str, 1024, fd))
    {
        cout<<str<<endl;
    }
    return 0;
}

A:

You're not getting the output from stderr in your program using popen.
Following simple example shows, that error stream from started application is printed in terminal, without being read in your program. popen redirects only stdout stream and stderr stream is not affected, so it's just printed to terminal.
#include <iostream>
#include <cstdio>

using namespace std;

int main ()
{
    FILE *fd = popen("errWr", "r");
    char str [1024];
    while (fgets(str, 1024, fd))
    {
        cout << "=>" << str << "<=" << endl;
    }
    return 0;
}

