?
Solved

redirect of stdout/stderr to stdin

Posted on 2003-02-24
7
Medium Priority
?
2,464 Views
Last Modified: 2009-12-16
I need to execute a program and redirect its stdout/stderr to the stdin. Can someone please tell me how to do this or point me in  the right direction.
0
Comment
Question by:Xcifer
[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
  • 4
  • 2
7 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8010701

Since you've posted the C group, I'm assuming that it's a C question.  Just to be sure, you can do it at the shell level in several ways.

ls | grep Text

Basically, the output from ls (written to stdout) is read by grep (via stdin).


Within a C program it's a different issue.

SaveOut = stdout;
SaveErr = stderr;

stdout = stdin;
stderr = stdin;


Now, everything written to stdout or stderr is piped back into the program.

Unless you're trying to capture the output from some embedded module I can't imagine why you'd want to do this.


Kdo
0
 

Author Comment

by:Xcifer
ID: 8010786
Yes, I am doing it from a c program.
Basically I wanted to do:
 system(program.exe);
And the get what values that this program sends to the stdout/stderr.

I figured I would have to redirect my streams but Im not sure how. Can you elaborate on the c programming.
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 8010799
>>And the get what values that this program sends to the stdout/stderr.

Aaah, now it makes sense to me :o)

Use '_popen()':

/* POPEN.C: This program uses _popen and _pclose to receive a
 * stream of text from a system process.
 */

#include <stdio.h>
#include <stdlib.h>

void main( void )
{

   char   psBuffer[128];
   FILE   *chkdsk;

        /* Run DIR 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( (chkdsk = _popen( "dir *.c /on /p", "rt" )) == NULL )
      exit( 1 );

   /* Read pipe until end of file. End of file indicates that
    * CHKDSK closed its standard out (probably meaning it
         * terminated).
    */
   while( !feof( chkdsk ) )
   {
      if( fgets( psBuffer, 128, chkdsk ) != NULL )
         printf( psBuffer );
   }

   /* Close pipe and print return value of CHKDSK. */
   printf( "\nProcess returned %d\n", _pclose( chkdsk ) );
}




(assuming that you are on Windows - if not, remove the trailing underscore...)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Xcifer
ID: 8010870
Thanks Again!
0
 

Author Comment

by:Xcifer
ID: 8011601
Hey I only tried it real quick and now that Im trying to fully use it, it doesnt seems to catch all the stdout/stderr.I know I already awrded you the points(jkr)but can you tell me if there is something else I need to do?
0
 

Author Comment

by:Xcifer
ID: 8011993
Hey I only tried it real quick and now that Im trying to fully use it, it doesnt seems to catch all the stdout/stderr.I know I already awrded you the points(jkr)but can you tell me if there is something else I need to do?
0
 
LVL 86

Expert Comment

by:jkr
ID: 8019407
Well, what of the output is missing? BTW, if you want the whole load of bells and whistles, see the sample at http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B190351 ("HOWTO: Spawn Console Processes with Redirected Standard Handles")
0

Featured Post

Independent Software Vendors: 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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

764 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