locked
POPEN: redirect Error Output RRS feed

  • Question

  • Hey there.

    A Question about popen: Is there any possibility to redirect the error output just like the stdoutr ?
    Wednesday, January 20, 2010 4:41 AM

Answers

  • Try invoking the called program using the stream redirection
    I showed you previously:

    if((pPipe = _popen("\"My Program\" 2>&1", "rt")) == NULL)
          exit( 1 );

    - Wayne
    • Marked as answer by Newbie79892 Thursday, January 21, 2010 8:52 PM
    Wednesday, January 20, 2010 10:08 PM

All replies

  • >Is there any possibility to redirect the error output
    >just like the stdout

    Maybe. I've never tried it with popen. But it depends on
    exactly what you're trying to accomplish. There are ways
    to redirect the stderr (cerr) stream to another stream:

    freopen, _wfreopen
    http://msdn.microsoft.com/en-us/library/wk2h68td(VS.80).aspx

    "freopen is typically used to redirect the pre-opened
    files stdin, stdout, and stderr to files specified by
    the user."

    _dup, _dup2
    http://msdn.microsoft.com/en-us/library/8syseb29(VS.80).aspx

    Excerpt:

    "The _dup and _dup2 functions associate a second file
    descriptor with a currently open file. These functions
    can be used to associate a predefined file descriptor,
    such as that for stdout, with a different file. ...
    The following example shows how to associate stderr
    (defined as FILE * in Stdio.h) with a file descriptor
    ,,,"

    --------------------------------------------------------------

    For example, here's a "quick and dirty" program which
    redirects stderr (cerr) to stdout (cout). You can see
    the effect if you run the program from a command line
    prompt in a console window. Run it normally and all
    three lines appear on the console. Run it with output
    (stdout) redirected to a file (prog > file) and the
    stderr output of the second line goes to the file.

    CAVEAT: No error checking is done of return values, etc.
    This should be added for real-world apps.

    #include "stdafx.h"
    #include <iostream>
    #include <io.h>

    using namespace std;

    int _tmain(int argc, _TCHAR* argv[])
    {
       cerr << "normal cerr\n";
       int oldstderr = _dup(fileno(stderr));
       _dup2(fileno(stdout), fileno(stderr));
       cerr << "cerr redirected to stdout\n";
       _dup2(oldstderr, fileno(stderr));
       cerr << "cerr back to normal\n";

       return 0;
    }

    E&OE

    Of course, from the command line you can redirect stderr
    of *any* program to stdout or a file by using the
    appropriate redirectors.

    Redirect stdout from prog to a file:
    prog > file
    prog >> file

    Redirect stderr from prog to a file:
    prog 2> file
    prog 2>> file

    Redirect stderr from prog to stdout:
    prog 2>&1

    Redirect stderr to stdout and then stdout/stderr to file:
    prog > file 2>&1
    prog >> file 2>&1

    In the last example all output from both stdout/cout and
    stderr/cerr goes to the named file.

    - Wayne
    Wednesday, January 20, 2010 7:32 AM
  • Well, I dont want to redirect to a file, I want to read the stderr of the fopened tool.
    Right now my programm (when started from commandline) reads the stdout, but the stderr is displayed on the console (so its kind of auto-redirected to grandparent process)
    Wednesday, January 20, 2010 5:57 PM
  • Well, I dont want to redirect to a file, I want to read the stderr of the fopened tool.
    Right now my programm (when started from commandline) reads the stdout, but the stderr is displayed on the console (so its kind of auto-redirected to grandparent process)
    Wednesday, January 20, 2010 6:02 PM
  • See if this thread helps:

    http://stackoverflow.com/questions/280571/

    - Wayne

    Wednesday, January 20, 2010 7:01 PM
  • Thx Wayne, but thats also about writing to a file, isnt it?....

    well, I know its possible somehow, because a C based Script language I use is able to ^^ - but how....
    Wednesday, January 20, 2010 8:56 PM
  • Try invoking the called program using the stream redirection
    I showed you previously:

    if((pPipe = _popen("\"My Program\" 2>&1", "rt")) == NULL)
          exit( 1 );

    - Wayne
    • Marked as answer by Newbie79892 Thursday, January 21, 2010 8:52 PM
    Wednesday, January 20, 2010 10:08 PM
  • stupid me, missed that point, sry wayne.

    However, it works!

    dont know whether to open a new threat or write it her, first I will wrote her, tell me, if a new threat may be better:

    usind CMD to start a programm, keystrokes I make are send to the application. How can I send "a keystroke" using a C(++) programm ?
    Thursday, January 21, 2010 6:51 PM
  • >However, it works!

    Glad to hear it. Please mark my last reply as the "Answer".

    >dont know whether to open a new threat or write it her

    Your original question has been answered. Please start a
    new thread for your new question about keystrokes.

    - Wayne
    Thursday, January 21, 2010 7:38 PM