469,593 Members | 1,877 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,593 developers. It's quick & easy.

Calling external program in C++

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

Jul 22 '05 #1
9 1721
Jon Slaughter wrote:
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
Jul 22 '05 #2
"Jon Slaughter" <no****@nowhere.com> ha scritto nel messaggio
news:10*************@corp.supernews.com...
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

Jul 22 '05 #3
Jon Slaughter wrote:
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
Jul 22 '05 #4

"Lemon tree" <le***@tree.it> wrote in message
news:M4**********************@news3.tin.it...
"Jon Slaughter" <no****@nowhere.com> ha scritto nel messaggio
news:10*************@corp.supernews.com...
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...


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
Jul 22 '05 #5

Mike Wahler wrote:
"Lemon tree" <le***@tree.it> wrote in message
news:M4**********************@news3.tin.it...
"Jon Slaughter" <no****@nowhere.com> ha scritto nel messaggio
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?


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

Jul 22 '05 #6

"msalters" <Mi*************@logicacmg.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

Mike Wahler wrote:
"Lemon tree" <le***@tree.it> wrote in message
news:M4**********************@news3.tin.it...
"Jon Slaughter" <no****@nowhere.com> ha scritto nel messaggio
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?


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
Jul 22 '05 #7
> 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::ostringstream;

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/...940ddf50edf667

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

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 22 '05 #8
> 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::ostringstream;

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/...940ddf50edf667

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

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 22 '05 #9
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::ostringstream;

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;
}

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Sivakumar Shanmugam | last post: by
11 posts views Thread by RichN | last post: by
1 post views Thread by notregister | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.