Solved

C++ redirect standard output for printf

Posted on 2003-11-14
2
1,151 Views
Last Modified: 2008-01-16
Hi
I am building a small command shell in C++ in a Unix environment and I want to be able to redirect standard IO.  I have figured out how to do this for cout statements, but is there a simple way to do it for printf statements as well?  Thank you.
0
Comment
Question by:k23m
2 Comments
 
LVL 5

Accepted Solution

by:
g0rath earned 50 total points
ID: 9750775

to a file?

FILE *stream;
int temp_fd;

// get a copy of the stdout file decriptor
temp_fd = dup(fileno(stdout));
// redirect stdout to a file
stream = freopen("redirect_file.txt", "w", stdout);

if (stream == NULL)
   exit(1);

printf("We're going to a file now\n");
// copy old stdout back, closing any open file descriptors while we're at it
dup2(temp_fd, fileno(stdout));
printf("We're going to the screen now\n");

0
 
LVL 86

Expert Comment

by:jkr
ID: 9750783
If you want to redirect/read the output of an external program, use 'popen()'. If you want to do that inside the code of a program, use 'freopen()', e.g.

/* FREOPEN.C: This program reassigns stderr to the file
 * named FREOPEN.OUT and writes a line to that file.
 */

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

FILE *stream;

void main( void )
{
   /* Reassign "stderr" to "freopen.out": */
   stream = freopen( "freopen.out", "w", stderr );

   if( stream == NULL )
      fprintf( stdout, "error on freopen\n" );
   else
   {
      fprintf( stream, "This will go to the file 'freopen.out'\n" );
      fprintf( stdout, "successfully reassigned\n" );
      fclose( stream );
   }
   system( "type freopen.out" );
}


0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now