Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c wrapper file

Posted on 2004-03-22
8
Medium Priority
?
652 Views
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!!!


0
Comment
Question by:jlm661
[X]
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
8 Comments
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 300 total points
ID: 10654817
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
 
LVL 23

Expert Comment

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

Assisted Solution

by:sunnycoder
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
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
Industry Leaders: 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!

 
LVL 9

Assisted Solution

by:ankuratvb
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
0
 
LVL 46

Accepted Solution

by:
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,
Kent
0
 

Author Comment

by:jlm661
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>

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

Kent
0
 

Author Comment

by:jlm661
ID: 10663466
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

Industry Leaders: 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

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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

704 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