?
Solved

C++ redirect standard output for printf

Posted on 2003-11-14
2
Medium Priority
?
1,230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
g0rath earned 150 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

801 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