Link to home
Start Free TrialLog in
Avatar of baharbh
baharbh

asked on

Executing executable with arguments

I have an executable file say "convert.exe"

The file is executed using the form

      1            2            3             4
  convert  -outfile  output_file  input_file

I am writing another C program that will access all files in the current directory and subject it to the
convert.exe program. From the above the arguments for 1 and 2 remains the same, 4 will be the name of the file being converted and 3 will have the same name as 4 only the extension is changed to say .out

Some basic info:

typedef struct {
      char fname[260];
} file_struct;

file_struct All_files[max_files] // max_files say defined to be 5000
.
.

in main()
.
   test_files  = get_files(All_files);      // test_files contains number of files to be converted

   for (i = 0; i < test_files; i++) {
      //printf("Processing file %d : %s\n",i, All_files[i].fname);
            system("convert ....)   // THE PART WHERE I DON'T KNOW WHAT STATEMENT(S) TO GIVE
   }
Avatar of shivsa
shivsa
Flag of United States of America image

system("./convert.exe output.file input.out input.in");
You can use sprintf to concoct the relevant string, then pass that to system. Something like:

char cmd[250];
char filename[150];
char *p;

for(i=0; i < test_files; i++)
{   printf("Processing file %d: %s\n",i,All_files[i].fname);
    strcpy(filename,All_files[i].fname);
    p=strrchr(filename,'.');  /* find '.' searching backwards from end
    *p='\0';
    sprintf(cmd,"convert -outfile %s.out %s",filename,All_files[i].fname);
}
ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kent Olsen
Hi baharbh,

It looks like you want to use the system() function.  Here's an example of how to get it to run convert.exe on several files:


char *InputFileList[] = {"File1.in", "File2.in", "File3.in", NULL};
char *OutputFilelist [] = {"File1.out", "File2.out", "File3,out", NULL};
char Buffer[1024];
int Fn;

  for (Fn = 0; InputFileList[Fn], Fn++)
  {
    sprintf (Buffer, "convert -outfile %s %s", OutputFileList[Fn], InputFileList[Fn]);  // Build the command string
    printf ("Executing command:  %s\n", Buffer);                                                 // Display the command being executed
    system (Buffer);                                                                                          // Run the command
  }


Good Luck,
Kent
or u can use execl which support arv passing.

execl(path, arg0, arg1, ...)
This is equivalent to "execv(path, (arg0, arg1, ...))".

If you want to use any of the exe..() family of APIs, you'll have to fork() a new process first.  These functions overlay (replace) the current process so a single task won't be able to issue multiple execl() function.  Of course, multiple tasks to do this is quite workable.  In fact, here is some code that will run all of the convert processes simultaneously.

char *InputFileList[] = {"File1.in", "File2.in", "File3.in", NULL};
char *OutputFilelist [] = {"File1.out", "File2.out", "File3,out", NULL};
char Buffer[1024];
int Fn;

  for (Fn = 0; InputFileList[Fn], Fn++)
  {
  if (fork()) // Parent task
    printf ("Beginning task to convert %s\n", InputFileList[Fn]);
  else
  {
    execl ("convert.exe", "-outfile", OutputFileList[Fn], InputFileList[Fn];
    "printf ("Finished converting %s\n", InputFileList[Fn]);
    break;               // Break the child task out of the for(;;) loop
  }


Again,
Good Luck,
Kent
Did any of those answers help?

If so, it is now time to select and grade them to close the question.

If not, perhaps a clarifying question would help.
Avatar of baharbh
baharbh

ASKER

Sorry fro late reply.

Shivsa - tried yr suggestion - gives a bad command/file name

Imladris - Yr suggestion worked perfectly. Will award you the points. Wd appreciate if u cd explain the magic behind the sprintf(cmd), system(cmd). Anyway thank you much.

Baharbh



You're welcome.

Can't tell you much beyond what the manuals do. The system function is in the standard library. It will take the argument and "pass" it to the operating system for execution. The sprintf function is also in the standard library. It works just like printf, except the result goes into the string supplied as the first argument, instead of to the screen.