hovnatan
hovnatan

Reputation: 1385

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;
}

Upvotes: 0

Views: 2689

Answers (1)

tmp
tmp

Reputation: 1127

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;
}

Upvotes: 2

Related Questions