Solved

How to call a seperate C program from another?

Posted on 1998-12-07
9
285 Views
Last Modified: 2010-04-15
Easy enough to do in COBOL, but I cannot find any examples of how it is done in C. Can someone please give me a code example of how I can call another C program (a completely self contained program that has its own main function), and check the results in the calling program.

Thank you for your help.
0
Comment
Question by:axd
9 Comments
 
LVL 5

Expert Comment

by:yonat
ID: 1254931
system("myprog.exe");
0
 
LVL 84

Expert Comment

by:ozo
ID: 1254932
In what manner are the results given?
system should return the exit status of the program.
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 1254933
>>In what manner are the results given?
>>system should return the exit status of the program.

Can save results in File and read from 1-st Programm.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 84

Expert Comment

by:ozo
ID: 1254934
If the results are giben on stdout, you might
 system("myprog.exe > File");
or perhaps
 File = popen("myprog.exe", "r");
0
 
LVL 1

Expert Comment

by:cmw
ID: 1254935
Use any of spawn.. functions (if you want to return to the calling program), or the exec.. if you want to transfer. These functions do not incure the memory overhead of loading the command processor (which system() does). You have the option of using the path, specifying a custom env, and picking up the result.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1254936
On the other hand, spawn is not part of ANSI C, so it may not be as portable as system
0
 
LVL 1

Expert Comment

by:cmw
ID: 1254937
True, but then that's what using C is all about. Most of my experience is with Intel and MS stuff, and of the 5 or 6 compilers I've really worked with, all had the functions. If you're tight on mem, chances are you have the spawn.. stuff. Using windows/Unix'es memory probably isn't your main consideration.
0
 
LVL 1

Expert Comment

by:sergelebel
ID: 1254938
keep in mind... if you need to pass multiple arguments to the child process, the spawn function is much more flexible...not to mention less memory ungry...
0
 
LVL 2

Accepted Solution

by:
hougaard earned 50 total points
ID: 1254939
// First Example - Spawn function

#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <errno.h>
#include <string.h>

void main()
  {
    int   process_id, status, rc;

    process_id = spawnl( P_NOWAIT, "child.exe",
             "child", "5", NULL );
    if( process_id == -1 ) {
      printf( "spawn failed - %s\n", strerror( errno ) );
      exit( EXIT_FAILURE );
    }
    printf( "Process id = %d\n", process_id );

#if defined(__OS2__) || defined(__NT__)
    rc = cwait( &status, process_id, WAIT_CHILD );
    if( rc == -1 ) {
      printf( "wait failed - %s\n", strerror( errno ) );
    } else {
      printf( "wait succeeded - %x\n", status );
      switch( status & 0xff ) {
      case 0:
      printf( "Normal termination exit code = %d\n",
            status >> 8 );
      break;
      case 1:
      printf( "Hard-error abort\n" );
      break;
      case 2:
      printf( "Trap operation\n" );
      break;
      case 3:
      printf( "SIGTERM signal not intercepted\n" );
      break;
      default:
      printf( "Bogus return status\n" );
      }
    }
#endif
    printf( "spawn completed\n" );
  }

/*
[child.c]
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>

void main( int argc, char *argv[] )
  {
    int delay;

    if( argc <= 1 ) exit( EXIT_FAILURE );
    delay = atoi( argv[1] );
    printf( "I am a child going to sleep "
          "for %d seconds\n", delay );
    sleep( delay );
    printf( "I am a child awakening\n" );
    exit( 123 );

  }
*/

For Windows you can use the WinExec()

UINT WinExec(
    LPCSTR lpCmdLine,      // address of command line
    UINT uCmdShow       // window style for new application
   );


Hope this helps!

Erik
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert image to byte array 8 201
C++ vs C compilers 13 159
Line meaning 9 86
Display a Float Variable in C without using the function printf. 2 12
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…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

777 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