how do i get to the system in C++?

T

Tamer Higazi

Hi!
I want to run an external Program in my C++ Program. I have a string of
an existing program which has to be executed. Which command do i have to
execute and in which library is it to find?

For anybody's help, Thank you.


Tamer
 
P

Peter van Merkerk

I want to run an external Program in my C++ Program. I have a string of
an existing program which has to be executed. Which command do i have to
execute and in which library is it to find?

The std::system() function in <cstdlib>seems to be what you are looking for.
 
T

Tamer Higazi

Dear Peter,
How do i run correctly the function?!

"Fullsting" is a variable of string.



FullString.assign(StringToConvert[0]);
FullString.append(StringToConvert[1]);
FullString.append(StringToConvert[2]);
system(FullString);

and output:

g++ CapiSuitePrint.C
CapiSuitePrint.C: In member function `void CapiPrint::convert()':
CapiSuitePrint.C:49: error: cannot convert `std::string' to `const
char*' for
argument `1' to `int system(const char*)'
tamer@linux:~/Desktop/CapiSuitePrint>

Tell me please, how i could avoid this error.


Thank you

Tamer
 
P

Peter van Merkerk

Tamer Higazi said:
Dear Peter,
How do i run correctly the function?!

"Fullsting" is a variable of string.

FullString.assign(StringToConvert[0]);
FullString.append(StringToConvert[1]);
FullString.append(StringToConvert[2]);
system(FullString);

This should work:

system(FullString.c_str());
 
R

Rolf Magnus

Tamer said:
Dear Peter,
How do i run correctly the function?!

"Fullsting" is a variable of string.



FullString.assign(StringToConvert[0]);
FullString.append(StringToConvert[1]);
FullString.append(StringToConvert[2]);

You could write this a bit cleaner with using operators:

FullString = StringToConvert[0];
FullString += StringToConvert[1];
FullString += StringToConvert[2];

or even:

FullString = StringToConvert[0] +
StringToConvert[1] +
StringToConvert[2];
system(FullString);

Try:

system(FullString.c_str());
 
D

Default User

Tamer said:
CapiSuitePrint.C: In member function `void CapiPrint::convert()':
CapiSuitePrint.C:49: error: cannot convert `std::string' to `const
char*' for
argument `1' to `int system(const char*)'
tamer@linux:~/Desktop/CapiSuitePrint>


Well, how do you get a const char* from a std::string? If you don't
know, look it up in your book. If you don't have a book, get one. If you
have one and it doesn't say, get a different one.



Brian Rodenborn
 
T

Tamer Higazi

Hi!
I don't understand why but i still got that problem. I just made a test
if the string could be executed correctly by just making

cout << "\n" << FullString;

and i tested this string manually and it worked.


StringToConvert[0] = "sfftobmp -t
/var/spool/capisuite/users/tamer/received/fax-";
StringToConvert[1] = static_cast<char>(CharNextNo);
StringToConvert[2] = ".sff
/var/spool/capisuite/users/tamer/received/ToPrint.tiff";

FullString.assign(StringToConvert[0]);
FullString.append(StringToConvert[1]);
FullString.append(StringToConvert[2]);
//cout << "\n" << FullString << "\n";
system(FullString.c_str());


Output:

tamer@linux:~/Desktop/CapiSuitePrint> ./a.out
Error open file.


I really don't know what to do now.

Rolf said:
Tamer Higazi wrote:

Dear Peter,
How do i run correctly the function?!

"Fullsting" is a variable of string.



FullString.assign(StringToConvert[0]);
FullString.append(StringToConvert[1]);
FullString.append(StringToConvert[2]);


You could write this a bit cleaner with using operators:

FullString = StringToConvert[0];
FullString += StringToConvert[1];
FullString += StringToConvert[2];

or even:

FullString = StringToConvert[0] +
StringToConvert[1] +
StringToConvert[2];

system(FullString);


Try:

system(FullString.c_str());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,167
Latest member
SusanaSwan
Top