MSDN > フォーラム ホーム > Visual C++ Language > How to redirect and log the output of the VC++ console to a file?
質問する質問する
 

回答済みHow to redirect and log the output of the VC++ console to a file?

  • 2009年11月5日 13:37techoptimist ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    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.

回答

  • 2009年11月6日 18:37techoptimist ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     回答済み
    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.

すべての返信

  • 2009年11月5日 18:28WayneAKing ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    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
  • 2009年11月5日 18:35techoptimist ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    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?
  • 2009年11月6日 18:37techoptimist ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     回答済み
    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.