Solved

c wrapper file

Posted on 2004-03-22
8
606 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
8 Comments
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 75 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 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
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
 
LVL 9

Assisted Solution

by:ankuratvb
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
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 45

Accepted Solution

by:
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,
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 45

Expert Comment

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 opening and reading files in the C programming language.

743 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

15 Experts available now in Live!

Get 1:1 Help Now