Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call .exe file within c++ program?

Tags:

c++

I want to use a exe file (convert.exe), inside my C++ program. This "exe" file change my output file format to another format. when, i use this convert.exe from my command-prompt (cmd), i have to type like this;

convert -in myfile -out convertedfile -n -e -h

where;

myfile= name of the file, I obtain from my c++ program convertedfile= result of the "convert.exe" file -n, -e, -h = are some parameters (columns) that i need to use to get output file with my desired data columns.

i tried with system(convert.exe). but, it does not work as i did not know how to use all those parameters.

like image 880
niro Avatar asked Mar 01 '11 14:03

niro


People also ask

What is .EXE file in C?

An executable file (EXE file) is a computer file that contains an encoded sequence of instructions that the system can execute directly when the user clicks the file icon.

How do I open a C EXE file?

Please try system("notepad"); which will open the notepad executable. Please note that the path to the executable should be part of PATH variable or the full path needs to be given to the system call.

Can you run .exe from CMD?

Type "start [filename.exe]" into Command Prompt, replacing "filename" with the name of your selected file. Replace "[filename.exe]" with your program's name. This allows you to run your program from the file path.


1 Answers

The std::system function expects a const char *, so how about you try

system("convert -in myfile -out convertedfile -n -e -h")

Then, if you want to be a little more flexible, you use std::sprintf can create a string with the right elements in it and then pass it to the system() function like so:

// create a string, i.e. an array  of 50 char
char command[50];  

// this will 'fill' the string command with the right stuff,
// assuming myFile and convertedFile are strings themselves
sprintf (command, "convert -in %s -out %s -n -e -h", myFile, convertedFile);   

// system call
system(command);
like image 109
dm76 Avatar answered Oct 26 '22 01:10

dm76