1

While debugging I want to display console output both on console and save a backup in file. Windows doesn't have tee, but you can add one. Say the folder is c:\bin\ and it works fine. And I have added it into system's PATH.

Problem is setting "[ ]| tee[.exe] output.txt" or " | tee[.exe] output.txt" won't work -- the output.txt is just nowhere to be found. I also tried to add the c:\bin\ path explicitly in VC Directories or environment under debugging and merge environment to be yes.

"> output.txt" works fine.

Anyone has any idea how I can resolve this? Many thanks!

Glenn Yu
  • 613
  • 1
  • 7
  • 11

2 Answers2

2

I assume that you're putting the | tee.exe output.txt string in the project property "Debugging | Command Argument".

Unfortunately, that property only supports the redirection operators, not the pipe operator. If you have the | tee.exe output.txt string in the preoperty and run a program that dumps the command line arguments, you'll see that that information is just passed on as the arguments. The "Debugging | Command Argument" doesn't actually get processed by a full-fledged shell (such as cmd.exe) - it's just the IDE supporting some simple redirection (actually, it seems to support more than I expected):

From http://msdn.microsoft.com/en-us/library/kcw4dzyf.aspx:

You can use the following redirection operators in this box:

    < file
        Reads stdin from file.

    > file
        Writes stdout to file.

    >> file
        Appends stdout to file.

    2> file
        Writes stderr to file.

    2>> file
        Appends stderr to file.

    2> &1
        Sends stderr (2) output to same location as stdout (1).

    1> &2
        Sends stdout (1) output to same location as stderr (2).

You can have a limited version of what you're looking for by redirecting the program's output to a file using >> and using a tail-f command to display whatever gets added to the file. If you do this you'll probably want to call setvbuf( stdout, NULL, _IONBF, 0 ) first thing in main() so that I/O is unbuffered. Otherwise tail -f won't see it until the buffer gets flushed, and I imagine that you'd like to see each output operation as it occurs.

Another option is to crank the console window's "Screen Buffer Height" property up to a large number - one of the first things I do when I get a new Windows machine is set that value to 3000 or so - then debug the program normally and copy/paste the contents of the console window before it closes.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
0

You better NOT use printf for this purpose. Instead, write your own function; taking formatted-input, like printf - having variable number of arguments (...). That function will use printf to display on console, get the buffer written on file, would send to output to debug window and all. You may customize it depending on Debug/Release build.

It may go like (may have some minor mistakes):

void PrintDebuggingInfo(const char* pFormatString, ...) 
{    
    va_list  arguments;

    char OutputString[1024];

    va_start(pFormatString, argument);    
    vsprintf(OutputString,  pFormatString, argument); // Generate string

   // Now use `OutputString` as you wish! 
}

You may use other variant of vsprintf. Infact all formatted-functions use this function only!

Ajay
  • 18,086
  • 12
  • 59
  • 105