Solved

C++ redirect standard output for printf

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 121
Find Visual Studio Tools 2 103
How to convert MFC APP to Win32 APP. 19 79
Beginner to Unreal Engine 4 5 80
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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. …
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…
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…

770 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