954,123 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Capturing Console Output

I would liek to invisibly run a console application, capture it's output, and save its contents into a log window with an edit control. I know it's possible becuase i've seen it done, but how can I capture terminal output?

BountyX
Posting Whiz in Training
230 posts since Mar 2004
Reputation Points: 28
Solved Threads: 9
 

i'm not sure if this is the answer u want, b/c i dont fully understand what u mean by "log window", but if u run a program from DOS prompt like:

./program >outfile standard output is redirected to the file outfile. if u want to do that inside the program, u use the dup2() function. open the output file, and then dup2 the descriptor onto STDOUT.

infamous
Junior Poster in Training
77 posts since Mar 2004
Reputation Points: 47
Solved Threads: 2
 

ugh, nevermind apparently windows doesn't support dup'ing

infamous
Junior Poster in Training
77 posts since Mar 2004
Reputation Points: 47
Solved Threads: 2
 
ugh, nevermind apparently windows doesn't support dup'ing

found a solution, ended up using pipes to redirect all streams

char psBuffer[128];
	 FILE *telnet;
	/* Run telnet so that it writes its output to a pipe. Open this
	* pipe with read text attribute so that we can read it 
	* like a text file. 
	*/
	 if( (telnet = _popen( "cdrecord --help", "rt" )) == NULL )
	 exit( 1 );
	 /* Read pipe until end of file. End of file indicates that 
	* telnet closed its standard out (probably meaning it 
	* terminated).
	*/
	 while( !feof( telnet ) )
	 {
	 if( fgets( psBuffer, 128, telnet ) != NULL )
	 printf( psBuffer );
	 }
	 /* Close pipe and print return value of telnet */
	 printf( "\nProcess returned %d\n", _pclose( telnet ) );


NOTE: I only used telnet for testing purposes, my real program created a blank file when I tried "cmd /c myProgram.exe > datafile.txt" with a system command or just "myProgram.exe > datafile.txt". The above was my solution.

BountyX
Posting Whiz in Training
230 posts since Mar 2004
Reputation Points: 28
Solved Threads: 9
 

I do not not understand how the pipeing program works

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 
I do not not understand how the pipeing program works

can i have the source if you have one, i want to capture the telnet outputs to a txt file, all i need to do it programatically, i need to login telnet and process some commands and get the output to a file, if i can get a c++ or vb source code which does this, it would be great, thanks in advance

sarin

sarin
Newbie Poster
1 post since Mar 2006
Reputation Points: 10
Solved Threads: 0
 
ugh, nevermind apparently windows doesn't support dup'ing

yes it does -- for redirecting stdout or stderr to a file. You can't redirect it like that to a MS-Windows GUI control, such as an edit control. And I don't think you can do that under *nix X11 controls either.

But I know what you want to do can be done -- I've seen it, but not sure how to do it either. Maybe pipes is the way to go.

Ancient Dragon
Retired & Loving It
Team Colleague
30,040 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

can i have the source if you have one, i want to capture the telnet outputs to a txt file, all i need to do it programatically, i need to login telnet and process some commands and get the output to a file, if i can get a c++ or vb source code which does this, it would be great, thanks in advance

sarin


hi,,

sarin u understand problem specified in that thread topic,
i m try also try to capture the telnet command prompt data into text file.. if u have code for this functionality then please help ...
i m very frustrate, i can't get solution .
plz provide solution as soon as possible...


krunal

krunalpatel1410
Newbie Poster
2 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You