• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 658
  • Last Modified:

c wrapper file

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!!!


0
jlm661
Asked:
jlm661
4 Solutions
 
Kyle AbrahamsSenior .Net DeveloperCommented:
you could just use the System command:

System("nsga2");
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

0
 
brettmjohnsonCommented:
This sounds like it would more appropriately be a shell script than a C program.
0
 
sunnycoderCommented:
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
http://www.cs.cf.ac.uk/Dave/C/node22.html#SECTION002230000000000000000

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
0
Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

 
ankuratvbCommented:
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
0
 
Kent OlsenData Warehouse Architect / DBACommented:
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,
Kent
0
 
jlm661Author Commented:
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>

main()
{
  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 :)
0
 
Kent OlsenData Warehouse Architect / DBACommented:
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.

Kent
0
 
jlm661Author Commented:
thanks kent! you rock!!!! it works! i might have some more quesitons later but right now i am good...THANK YOU!!

jess
0

Featured Post

SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now