Solved

C++ redirect standard output for printf

Posted on 2003-11-14
2
1,177 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

840 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