Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 369
  • Last Modified:

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
   }
0
baharbh
Asked:
baharbh
  • 4
  • 2
  • 2
  • +1
1 Solution
 
shivsaCommented:
system("./convert.exe output.file input.out input.in");
0
 
imladrisCommented:
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
 
imladrisCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
shivsaCommented:
or u can use execl which support arv passing.

execl(path, arg0, arg1, ...)
This is equivalent to "execv(path, (arg0, arg1, ...))".
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
imladrisCommented:
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
 
baharbhAuthor Commented:
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
 
imladrisCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now