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
Solved

system() usage: I need to capture both the return code and the output

Posted on 2002-07-16
2
248 Views
Last Modified: 2010-04-15
sounds simple... I hope so :-)

the system() command returns an int status code when it executes any command.. I need that so I can tell my calling function if the system() command was successful.  But the commands that will be running will also have stdout and/or stderr output that I need to capture and return to my caller.

Using system(), an answer would be to redirect output to a standard file by adding " >fileX 2>fileY " to the command, then reading those files afterwards.  But this worries me because I don't know what commands might be requested, and somebody might just try to redirect output themselves...

I also considered popen().. where I can easily capture the stdout.  But popen doesn't tell me if the command failed, nor does it provide a function for stderr.

Can anyone recommend a solution?

Thanks, George
0
Comment
Question by:gljr
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 7157845
Use 'popen()' instead of system:

/* 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 ) );
}

PS: If you're not on Win32, remove the leading underscores from '_popen()' and '_pclose()'.
0
 

Author Comment

by:gljr
ID: 7157940
MAGNIFICO!!!!

I had already written a routine using popen(), but it never
dawned on me to do a pclose(), let alone that the return from that value would indicate success/failure of the command in question.

You just made my day :-)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

856 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