Solved

Executing executable with arguments

Posted on 2003-11-20
9
317 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
  • 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
 
LVL 45

Expert Comment

by:Kdo
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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 is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-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.

758 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

22 Experts available now in Live!

Get 1:1 Help Now