Visual C++ Developer Center > Visual C++ Forums > Visual C++ Language > How to redirect and log the output of the VC++ console to a file?
Ask a questionAsk a question
 

AnswerHow to redirect and log the output of the VC++ console to a file?

  • Thursday, November 05, 2009 1:37 PMtechoptimist Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have developed an application(managed + unmanaged) in VC++ using Visual Studio 2005. IT can switch to either Console or to windows form depending upon the no of parameters passed to the application on command prompt console. Its basicall a windows application but console has been implemented with the help of Attachconsole(), allocconsole() & freeconsole().

    Now if i run a batch file, using command prompt console, all the output messages of the application can be seen on the console as shown below.

    c:\test> testapp -f filename -l operationname -t otherparams
    filename selected..
    operation in progress..
    operation completed successfully!!

     But if i redirect the output of the console to a text file, i dont see any messages in the text file once the operation is complete.
    for eg:

    c:\test> testapp -f filename -l operationname -t otherparams >> logfile.txt

    Clarifications i need are
    1> am i missing something to log it in a text file?
    2> Can output messages from the console be logged in such a fashion. if yes, how?

    Thanks in advance for help/suggestions on the same.

Answers

  • Friday, November 06, 2009 6:37 PMtechoptimist Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Getting the impression that either VC++ has been phased out or no takers for VC++.  NO responses for all 3(different) of my posts.

    Anyway, found out myself that Console created along with windows form is not the standard console we use in Win32 applications. Hence redirection of stder, stdout & stdin can be done by using Console::SetError, Console::Setout & Console::Setin correspondingly. If redirection to be used to log the stdout, then Streamwriter() has to be used i.e

    Streamwriter^ pStream = gcnew Streamwriter(log.txt)
    Console::Setout(pStream );  <---------This configures the stdout

    now on anything written using Console::Write() or Console::Writeline() is written to log.txt

    Similarly, stdin and stder can be configured and used too.
    • Marked As Answer bytechoptimist Friday, November 06, 2009 6:37 PM
    •  

All Replies

  • Thursday, November 05, 2009 6:28 PMWayneAKing Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Are you saying that the lines do not appear *anywhere* when you use redirection?
    Not in the file and not on the screen?

    What method are you using to send the messages to the console? (printf(), other?)

    - Wayne
  • Thursday, November 05, 2009 6:35 PMtechoptimist Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yeah, I am not able to see the output messages of the application neither on console nor in the redirected text file.

    Am using 

    //to open the console
    if(!AttachConsole(ATTACH_PARENT_PROCESS))
    AllocConsole();

    //to print it on the console.
    console::Write() 

    But peculiar thing is , if i dont redirect the messages to a text file, i can see the messages on the console.

    Any hint?
  • Friday, November 06, 2009 6:37 PMtechoptimist Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Getting the impression that either VC++ has been phased out or no takers for VC++.  NO responses for all 3(different) of my posts.

    Anyway, found out myself that Console created along with windows form is not the standard console we use in Win32 applications. Hence redirection of stder, stdout & stdin can be done by using Console::SetError, Console::Setout & Console::Setin correspondingly. If redirection to be used to log the stdout, then Streamwriter() has to be used i.e

    Streamwriter^ pStream = gcnew Streamwriter(log.txt)
    Console::Setout(pStream );  <---------This configures the stdout

    now on anything written using Console::Write() or Console::Writeline() is written to log.txt

    Similarly, stdin and stder can be configured and used too.
    • Marked As Answer bytechoptimist Friday, November 06, 2009 6:37 PM
    •