Solved

Executing executable with arguments

Posted on 2003-11-20
9
348 Views
Last Modified: 2010-04-15
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
   }
0
Comment
Question by:baharbh
[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
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 24

Expert Comment

by:shivsa
ID: 9789535
system("./convert.exe output.file input.out input.in");
0
 
LVL 16

Expert Comment

by:imladris
ID: 9789548
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);
}
0
 
LVL 16

Accepted Solution

by:
imladris earned 300 total points
ID: 9789554
Sorry, need the system command too.....

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);
    system(cmd);
}
0
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!

 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9789564
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
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9789576
or u can use execl which support arv passing.

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

Expert Comment

by:Kent Olsen
ID: 9789686

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
0
 
LVL 16

Expert Comment

by:imladris
ID: 9797933
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.
0
 

Author Comment

by:baharbh
ID: 9800572
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



0
 
LVL 16

Expert Comment

by:imladris
ID: 9811538
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.
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

Suggested Solutions

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

733 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