• Advertisement
Sign in to follow this  

[C++/Win32] Dumping console output

This topic is 3953 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, Just wondering how I could go about dumping the console output of my program to a file when it finishes execution. When I say console output I'm referring to anything that appears in the console window during program execution (e.g. text from std::cout, std::cerr etc.) I'm pretty certain it's possible using the Windows Console Functions, but I don't know how to do it exactly. I believe it has something to do with the console (screen) buffer but am not sure if it is trivial to do. Any help is appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Use a pipe from the command line. That's the easiest imo.

myexe > output.txt

The output won't be echoed to the console. It will be directed into the file.

Share this post


Link to post
Share on other sites
Quote:
Original post by LessBread
Use a pipe from the command line. That's the easiest imo.

myexe > output.txt

The output won't be echoed to the console. It will be directed into the file.


Thanks for the reply.

Actually, I need to echo the output to the console as well. Is there a way I can do that and pipe the output too?

Share this post


Link to post
Share on other sites
I've done a bit more research and it looks like the unix tee command is exactly what I need.

Unfortunately this isn't available on Windows, so I was wondering if there's a way to mirror the functionality (otherwise I can just download a unix port, but I'd prefer something native to Win32 if possible).

Share this post


Link to post
Share on other sites
For fun, I decided to write my own version of tee after I saw this post. Tested on Linux only, but should run just fine on Windows as well. Just start a new Console project in Visual Studio and run it through. It's really crazy how simple some of those basic Unix commands are.


/** @file mytee.c
* @author Brad Fish <brad.fish@gmail.com>
* A quick implementation of "tee" off the top of my head.
* I'm not responsible if this makes your computer explode.
*/


#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

int main (int argc, char *argv[]);

int main (int argc, char *argv[])
{
FILE *output;
int c;

if (argc < 2)
{
fprintf(stderr, "Usage: mytee <filename>\n");
return -1;
}

if (!(output = fopen(argv[1], "wb")))
{
fprintf(stderr, "%s\n", strerror(errno));
return -2;
}

while (1)
{
if ((c = fgetc(stdin)) == EOF)
break;
fputc(c, output);
fputc(c, stdout);
}

fclose(output);
}





Of course, this could probably be improved quite a bit by using fread/fwrite to read and write multiple bytes at once, if available.

...after a few minutes...

OK, here's my new and improved version that uses buffered I/O.


/** @file mytee.c
* @author Brad Fish <brad.fish@gmail.com>
* A quick implementation of "tee" off the top of my head.
* I'm not responsible if this makes your computer explode.
*/


#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

int main (int argc, char *argv[]);

int main (int argc, char *argv[])
{
FILE *output;
char buf[512];
int read;

if (argc < 2)
{
fprintf(stderr, "Usage: mytee <filename>\n");
return -1;
}

if (!(output = fopen(argv[1], "wb")))
{
fprintf(stderr, "%s\n", strerror(errno));
return -2;
}

while (1)
{
read = fread(buf, sizeof(char), 512, stdin);
if (!read && feof(stdin))
break;
fwrite(buf, sizeof(char), read, output);
fwrite(buf, sizeof(char), read, stdout);
}

fclose(output);
}





A slightly more complicated method of achieving this without an external command/program would be to redirect cout and cerr by replacing the streambufs for those objects using the rdbuf member function. A short example is here: http://www.cplusplus.com/reference/iostream/ios/rdbuf.html. To write to stdout/stderr AND a file would require you to write a custom streambuf.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement