Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to compare stderr to stdout

How to compare stderr to stdout

Scheduled Pinned Locked Moved C / C++ / MFC
c++htmlcomtutorialquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DQNOK
    wrote on last edited by
    #1

    In a console app, I want to be able to test whether stdout and stderr are both printing to the screen. My output is messy unless I get it right (I'm printing to both stdout, and to stderr, and if they both happen to be the screen, I get two copies; I only want one copy on the screen). A line like if( stdout == stderr ) always tests false under all the Windows compilers I've tried, so I get the duplication I'm trying to avoid. Any ideas?

    David --------- Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code. http://yosefk.com/c++fqa/picture.html#fqa-6.6 ---------

    D N M 3 Replies Last reply
    0
    • D DQNOK

      In a console app, I want to be able to test whether stdout and stderr are both printing to the screen. My output is messy unless I get it right (I'm printing to both stdout, and to stderr, and if they both happen to be the screen, I get two copies; I only want one copy on the screen). A line like if( stdout == stderr ) always tests false under all the Windows compilers I've tried, so I get the duplication I'm trying to avoid. Any ideas?

      David --------- Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code. http://yosefk.com/c++fqa/picture.html#fqa-6.6 ---------

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      DQNOK wrote:

      A line like if( stdout == stderr ) always tests false under all the Windows compilers I've tried, so I get the duplication I'm trying to avoid.

      What about:

      if (&stdout == &stderr)

      In any case, I would really expect them to not ever be *equal* even though they may resolve to the same location (i.e., screen).

      "Love people and use things, not love things and use people." - Unknown

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      D 1 Reply Last reply
      0
      • D David Crow

        DQNOK wrote:

        A line like if( stdout == stderr ) always tests false under all the Windows compilers I've tried, so I get the duplication I'm trying to avoid.

        What about:

        if (&stdout == &stderr)

        In any case, I would really expect them to not ever be *equal* even though they may resolve to the same location (i.e., screen).

        "Love people and use things, not love things and use people." - Unknown

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        D Offline
        D Offline
        DQNOK
        wrote on last edited by
        #3

        DavidCrow wrote:

        What about: if (&stdout == &stderr)

        That won't compile under Visual C++ The standard says that stdout and stderr are macros that evaluate to FILE pointer rvalues. Taking the address them seems suspicious from the outset...

        David --------- Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code. http://yosefk.com/c++fqa/picture.html#fqa-6.6 ---------

        1 Reply Last reply
        0
        • D DQNOK

          In a console app, I want to be able to test whether stdout and stderr are both printing to the screen. My output is messy unless I get it right (I'm printing to both stdout, and to stderr, and if they both happen to be the screen, I get two copies; I only want one copy on the screen). A line like if( stdout == stderr ) always tests false under all the Windows compilers I've tried, so I get the duplication I'm trying to avoid. Any ideas?

          David --------- Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code. http://yosefk.com/c++fqa/picture.html#fqa-6.6 ---------

          N Offline
          N Offline
          Nibu babu thomas
          wrote on last edited by
          #4

          DQNOK wrote:

          Any ideas?

          Does GetStdHandle() help?

          Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

          D 2 Replies Last reply
          0
          • D DQNOK

            In a console app, I want to be able to test whether stdout and stderr are both printing to the screen. My output is messy unless I get it right (I'm printing to both stdout, and to stderr, and if they both happen to be the screen, I get two copies; I only want one copy on the screen). A line like if( stdout == stderr ) always tests false under all the Windows compilers I've tried, so I get the duplication I'm trying to avoid. Any ideas?

            David --------- Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code. http://yosefk.com/c++fqa/picture.html#fqa-6.6 ---------

            M Offline
            M Offline
            maxie2005
            wrote on last edited by
            #5

            If your output is being flooded why dont you just re-direct stderr to a file. console_app 2> stderr.log

            D 1 Reply Last reply
            0
            • M maxie2005

              If your output is being flooded why dont you just re-direct stderr to a file. console_app 2> stderr.log

              D Offline
              D Offline
              DQNOK
              wrote on last edited by
              #6

              I like that idea. But this needs to be general. It's an "alert" library (a very minature logging library) that needs to detect on its own whether stderr has already been redirected. Key idea: *auto-detect* where stderr is outputting to, and if it's going to stdout, skip over the requirement to write to stderr.

                 vprintf( alertMsg, msgArgs );             //print to stdout
                 if( stdout != stderr )  
                    vfprintf( stderr, alertMsg, msgArgs ); //print to stderr
               //otherwise, already printed to stderr.  Just move on.
                 ...
              

              (Note: code is not complete; it needs some complicating va_end and va_starts in there to make it work correctly. Don't use it as a model!)

              David --------- Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code. http://yosefk.com/c++fqa/picture.html#fqa-6.6 ---------

              1 Reply Last reply
              0
              • N Nibu babu thomas

                DQNOK wrote:

                Any ideas?

                Does GetStdHandle() help?

                Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

                D Offline
                D Offline
                DQNOK
                wrote on last edited by
                #7

                Nibu babu thomas wrote:

                Does GetStdHandle() help?

                I'll look into it. Thanks for the idea.

                David

                1 Reply Last reply
                0
                • N Nibu babu thomas

                  DQNOK wrote:

                  Any ideas?

                  Does GetStdHandle() help?

                  Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

                  D Offline
                  D Offline
                  DQNOK
                  wrote on last edited by
                  #8

                  Nibu babu thomas wrote:

                  Does GetStdHandle() help?

                  From reading the MSDN site, I thought it would; but it doesn't do what I thought.

                     HANDLE  stderrHndl = GetStdHandle(STD_ERROR_HANDLE);
                     HANDLE  stdoutHndl = GetStdHandle(STD_OUTPUT_HANDLE);
                  
                  ...
                  (very incomplete!)
                  
                     vfprintf( stderr, msg, args );
                  
                     if( stderrHndl == stdoutHndl ) //still ALWAYS tests FALSE
                        goto logprint; //skip printing to stdout
                  
                     vfprintf( stdout, msg, args );
                  logprint:
                  

                  Because the stderrHndl == stdoutHndl is always evaluating to FALSE, I'm still double printing to the console.

                  David

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups