Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calling external program in C++

Reply
Thread Tools

Calling external program in C++

 
 
Jon Slaughter
Guest
Posts: n/a
 
      12-13-2004
is there a C++ method of calling an external executable program? I know you
can do it using C, but what about C++?



 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-13-2004
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
 
Reply With Quote
 
 
 
 
Lemon tree
Guest
Posts: n/a
 
      12-13-2004
"Jon Slaughter" <> ha scritto nel messaggio
news:...
> 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



 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      12-13-2004
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
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      12-14-2004

"Lemon tree" <> wrote in message
news:M4ovd.289860$...
> "Jon Slaughter" <> ha scritto nel messaggio
> news:...
> > 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


 
Reply With Quote
 
msalters
Guest
Posts: n/a
 
      12-14-2004

Mike Wahler wrote:
> "Lemon tree" <> wrote in message
> news:M4ovd.289860$...
> > "Jon Slaughter" <> 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

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      12-14-2004

"msalters" <> wrote in message
news: oups.com...
>
> Mike Wahler wrote:
> > "Lemon tree" <> wrote in message
> > news:M4ovd.289860$...
> > > "Jon Slaughter" <> 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


 
Reply With Quote
 
jcoffin@taeus.com
Guest
Posts: n/a
 
      12-15-2004
> 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: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/...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.

 
Reply With Quote
 
jcoffin@taeus.com
Guest
Posts: n/a
 
      12-15-2004
> 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: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/...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.

 
Reply With Quote
 
jcoffin@taeus.com
Guest
Posts: n/a
 
      12-15-2004
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: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;
}

--
Later,
Jerry.

The universe is a figment of its own imagination.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling external program in java code hrn Java 6 09-09-2009 03:13 AM
calling an external program and capturing the output Eric Python 3 01-18-2009 06:30 PM
Calling external program from within python Emmanouil Angelakis Python 10 07-25-2008 09:53 PM
Calling external program from browser with parameters - cross-browser Jeff ASP .Net 13 10-06-2007 08:03 AM
calling external program from within C++ Juggler C Programming 5 02-24-2005 07:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57