Go Premium for a chance to win a PS4. Enter to Win


c wrapper file

Posted on 2004-03-22
Medium Priority
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 it...do i have to include <> all of those?

thanks for all of your help!!!

Question by:jlm661
LVL 41

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 300 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 400 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

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Assisted Solution

ankuratvb earned 300 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
LVL 46

Accepted Solution

Kent Olsen earned 1000 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 program...it 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 46

Expert Comment

by:Kent Olsen
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

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

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…
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…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

824 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