Calling external program in C++

J

Jon Slaughter

is there a C++ method of calling an external executable program? I know you
can do it using C, but what about C++?
 
V

Victor Bazarov

Jon said:
is there a C++ method of calling an external executable program? I know you
can do it using C, but what about C++?

Use the same way you know. C++ is often quite compatible with C in that
area. If you encounter a problem, come back and let's talk about it.

V
 
L

Lemon tree

Jon Slaughter said:
is there a C++ method of calling an external executable program? I know you
can do it using C, but what about C++?

I think that there is no standard way to do this...

So, in my opinion, the best way is to use the C -stylepossibly wrapped
inside a C++ method or something similar.

Andrea Sini
 
D

Default User

Jon said:
is there a C++ method of calling an external executable program? I
know you can do it using C, but what about C++?

Same way, system().




Brian
 
M

Mike Wahler

Lemon tree said:
I think that there is no standard way to do this...

Yes there is. Standard function 'system()'. But note
that its argument and actual functionality are implementation-
specific (e.g. not all implementations have the necessary access
to a command processor.
So, in my opinion, the best way is to use the C -style


'system()' is as much a part of the C++ standard library as
it is of the C standard library.
possibly wrapped
inside a C++ method or something similar.

Why wrap it?

-Mike
 
M

msalters

Mike said:
Yes there is. Standard function 'system()'. But note
that its argument and actual functionality are implementation-
specific (e.g. not all implementations have the necessary access
to a command processor.


'system()' is as much a part of the C++ standard library as
it is of the C standard library.


Why wrap it?

Wrap it so you can add arguments more easily; the C interface
of system has a horrible way to add arguments (memory fiddling).
std::string has a decent operator+(). So even
void system( std::string const& );
is a useful wrapper.

Regards,
Michiel Salters
 
M

Mike Wahler

msalters said:
Wrap it so you can add arguments more easily;

Huh? 'system()' takes exactly one argument.

the C interface
of system has a horrible way to add arguments (memory fiddling).

Huh? 'system()' takes exactly one argument, and it is provided
the same as for any other function. No 'fiddling' needed.
std::string has a decent operator+().

So you want to concatentate strings. You can do this easily for
C-style strings with 'strcat()'.
So even
void system( std::string const& );
is a useful wrapper.

It would let you pass a std::string object, yes. How useful
that might be is imo a matter of opinion. One can already
supply the contents of a std::string to 'system()' via
std::string::c_str().

-MIke
 
J

jcoffin

is there a C++ method of calling an external executable program?
I know you can do it using C, but what about C++?

Here's one possibility you might find interesting (this was a quick
hack at overhauling some code I wrote _years_ ago that used an
ostrstream, so it might benefit from some cleanup):

#include <sstream>
#include <cstdlib>

using std::eek:stringstream;

ostringstream &execute(ostringstream &s)
{
std::system(s.str().c_str());
return s;
}

ostringstream &operator<<(ostringstream &s,
ostringstream &(*manip)(ostringstream &s))
{
return manip(s);
}

int main() {
ostringstream x;

x << "ls " << execute;

return 0;
}

If you want to see the original code, it's at:

http://groups-beta.google.com/group/comp.lang.c++/msg/a2940ddf50edf667

I have to admit that until I looked at the date on that post, I hadn't
quite realized how old this really was. :)
 
J

jcoffin

is there a C++ method of calling an external executable program?
I know you can do it using C, but what about C++?

Here's a possibility you might find interesting:

#include <sstream>
#include <cstdlib>

using std::eek:stringstream;

ostringstream &execute(ostringstream &s) {
std::system(s.str().c_str());
return s;
}

ostringstream &operator<<(ostringstream &s,
ostringstream &(*manip)(ostringstream &s))
{
return manip(s);
}

int main() {
ostringstream x;

x << "ls ";
x << execute;

return 0;
}

This is a quick hack of some much older code that used an strstream; it
might benefit from further work. In case you case, the original code is
here:

http://groups-beta.google.com/group/comp.lang.c++/msg/a2940ddf50edf667

I must confess that I hadn't realized just HOW old this code was until
I looked at the date on that post...
 
J

jcoffin

Okay, I'll make _one_ more try at getting this posted -- as they say,
third time pays for all....
is there a C++ method of calling an external executable program?
I know you can do it using C, but what about C++?

Perhaps you'll find this an interesting possibility:

#include <sstream>
#include <cstdlib>

using std::eek:stringstream;

ostringstream &execute(ostringstream &s) {
std::system(s.str().c_str());
return s;
}

ostringstream &operator<<(ostringstream &s,
ostringstream &(*manip)(ostringstream &s))
{
return manip(s);
}

int main() {
ostringstream x;

x << "ls ";
x << execute;

return 0;
}
 

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,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top