Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CAN BurtanI OR anyone help with capturing everything in the dos box!!!

Status
Not open for further replies.

pilg

Programmer
Feb 16, 2001
82
0
0
GB
I have a C++ program that displays a lot of information in the dos box, all this information is needed to refer back to, to make adjustments. basicly is there a way of capturing everything in the dos box and directing it at a file. I cant use standard output as this does not work for some reason.
 
If your program is a win32 console mode application,
you can redirect standard output stream by using 'dup2'.

dup( fd, 1 );

will redirects standard output to file 'fd'.
Hee S. Chung
heesc@netian.com
 
Sorry, I made a slight mistake...
dup( fd, 1 ); ----> dup2( fd, 1 )


And an example code for dup2 is


/* dup2 sample */

#include <stdio.h>
#include <io.h>

int main( void )
{
FILE* fp = fopen( &quot;myfile&quot;, &quot;w&quot; );
if( fp ) {

/* reserve old stdout */
int oldstdout = dup(1);

/* redirect stdout */
dup2( fileno(fp), 1 );

/* the following message will be written in the file 'myfile' */
puts( &quot;My favorite musician is Freddie Mercury...&quot; );

/* flush intermediate buffer for standard output */
fflush( stdout );

fclose( fp );

/* recover old stdout */
dup2( oldstdout, 1 );

/* the following message will be printed in the screen */
puts( &quot;He is the greatest musician!&quot; );
}

return 0;
}

/* end of source */


You can check the return value of dup or dup2 because it returns -1 if it is failed.

I wonder this function can help you...
Hee S. Chung
heesc@netian.com
 
Hi,
I want to know if this dup construction could be applied for input, I mean, that if an app that read data from keyboard would read data from a file instead and whether such a manipulation could be imposed from a parent process.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top