c wrapper file

Posted on 2004-03-22
Last Modified: 2010-04-15
hello all! i feel like what i am trying to do is easy i am just unsure of how to do it.

what i want to do is create a c-wrapper file to run through a series of commands so that once the first program is complete pro/e will run. here are the commands i would type

1.) nsga2
2.) proewildfire trail2.txt

now with the nsga2 there are a lot of header files associated with i have to include <> all of those?

thanks for all of your help!!!

Question by:jlm661
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 75 total points
ID: 10654817
you could just use the System command:

System("proewildfire trail2.txt");

as far as headers you shouldn't have to, if/once it's compiled it should have it's own .exe

LVL 23

Expert Comment

ID: 10654875
This sounds like it would more appropriately be a shell script than a C program.
LVL 45

Assisted Solution

sunnycoder earned 100 total points
ID: 10655252
Hi jlm661,

What I could gather from your question was that you have some executables which you would like to execute in a predefined sequence and you wish to do that from a C program.

There are three ways of doing it from a C program

1. as ged325 showed, use system()
        system ( "nsga2" );
  the system command will invoke your default command interpreter shell and tell it to execute the command that you specified in " " .... you do not have to include any header files used in nsga2

2. use popen .... If you require unidrectional communication with the executable launched, use popen ... It works exactly like system but it also gives you a handle to a pipe created between your program and the executable launched

3. use fork/exec on windows or CreateProcess on windows .... this is the safest method to do what you require  ... add a wait() in parent thread to ensure sequential execution ... here is a link to refer

Lastly, brettmjohnson is very right in saying that these kind of things are better suited for a shell script or a batch script rather than a C program

Assisted Solution

ankuratvb earned 75 total points
ID: 10656470
Also what u can do,
is in ur nsga program,
in main(),just add a command at the end after everything has been executed.
system("proe....ur command");
u can also place this command in the at_exit() function
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.

LVL 45

Accepted Solution

Kdo earned 250 total points
ID: 10657160
Hi jlm661,

Some good suggestions here.....

The ideal place to control this is with your operating system's scripting language.  That's what it's designed to do.

But if you want near 100% portability, you can certainly have your C program run the other commands.  For the most part, this will keep you from having to convert a shell script for each system.  Though you may have to have different "command files" for each operating system.  (Among the things that will have to be accounted for are file names.  Unix uses the slash '/' character to separate directory/file names.  Windows uses the backslash '\'.)

Here' a VERY simple program to execute the command lines in a file.  It's NOT a shell.  It doesn't handle if, while, exit, etc commands, but it does let you run programs according to your own input file.

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

#include MAX_LINE  400   /*  maximum input line length  */

FILE *input;      /*  stream to read the command file  */

int main (int argc, char **argv)
  int PNum;
  int ReturnValue;
  char SourceLine[MAX_LINE];

  for (PNum = 1; PNum < argc - 2; PNum++)   /*  Process all the parameters as separate file names  */
    input = fopen (argv[PNum);
    if (input == NULL)
      fprintf (stderr, "Could not open file %s.  Exiting.\n", argv[PNum]);
      exit (0);
    while (fgets (SourceLine, MAX_LINE, input))  /*  Read a command from the file  */
       ReturnValue = system (SourceLine);         /*  You might want to skip comment lines  */
       if (ReturnValue)                                      /*  Quit executing the script if an error occurs  */
          fprintf (stderr, " Error %d occurred while executing command \"%s\"\n", ReturnValue, SourceLine);
          fclose (input);
          exit (0);
    fclose (input);
  fprintf (stderr, " Done.\n");

Good Luck,

Author Comment

ID: 10661381
okay so i have tried to create a simple c compiles and runs but there is no output...for example:

/*this C-wrapper file will execute first the nsga2 executable file and then run pro/e using the trail.txt file*/

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

  printf("Files in Directory are:\n");
             execl("/home/edog.1/jlm661/nsga/GA/","ls", "-l",0);
  //printf("The following output describes the NSGA optimization process:\n");
             //execl("/home/edog.1/jlm661/nsga/GA/","nsga2", 0);
  //printf("Pro/E will now run using the trail.txt file:n");
             //execl("/home/edog.1/", "proewildfire trail.txt", 0);

i am just trying to show the list of the files contained in my folder, but when i run the executable nothing but the line i printed comes up. what am i donig wrong? i probably look like a newbie but i am so bear with me :)
LVL 45

Expert Comment

ID: 10662775
Hi jlm661,

execl() is NOT the same as system().  system() spawns another task and executes the command string that you pass to it.  execl() replaces the current task with whatever the execl() is supposed to do.

Change the calls to execl() to system().  You'll also have to build the command string into a single string.


Author Comment

ID: 10663466
thanks kent! you rock!!!! it works! i might have some more quesitons later but right now i am good...THANK YOU!!


Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with form project 66 316
C Language combined operators 28 106
Problem to show menu 5 87
What is atomic operation? 6 43
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 recursion in the C programming language.

947 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now