Solved

Executing executable with arguments

Posted on 2003-11-20
9
328 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
Are your corporate email signatures appalling?

Is it scary how unprofessional your email signatures look? Do users create their own terrible designs and give themselves stupid job titles? You can make this a lot easier for yourself by choosing an email signature management solution from Exclaimer today.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
port mapped I/o query 10 166
memory mapped I/O query 6 142
Pointer in one class to member in another 6 116
Need example 5 107
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

895 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