Calling external program in C++

Discussion in 'C++' started by Jon Slaughter, Dec 13, 2004.

  1. is there a C++ method of calling an external executable program? I know you
    can do it using C, but what about C++?
     
    Jon Slaughter, Dec 13, 2004
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Dec 13, 2004
    #2
    1. Advertising

  3. Jon Slaughter

    Lemon tree Guest

    "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
     
    Lemon tree, Dec 13, 2004
    #3
  4. Jon Slaughter

    Default User Guest

    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
     
    Default User, Dec 13, 2004
    #4
  5. Jon Slaughter

    Mike Wahler Guest

    "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
     
    Mike Wahler, Dec 14, 2004
    #5
  6. Jon Slaughter

    msalters Guest

    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
     
    msalters, Dec 14, 2004
    #6
  7. Jon Slaughter

    Mike Wahler Guest

    "msalters" <> wrote in message
    news:...
    >
    > 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
     
    Mike Wahler, Dec 14, 2004
    #7
  8. Jon Slaughter

    Guest

    > 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. :)

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    , Dec 15, 2004
    #8
  9. Jon Slaughter

    Guest

    > 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...

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    , Dec 15, 2004
    #9
  10. Jon Slaughter

    Guest

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

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    , Dec 15, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juggler

    calling external program from within C++

    Juggler, Feb 24, 2005, in forum: C Programming
    Replies:
    5
    Views:
    602
    Nils Weller
    Feb 24, 2005
  2. Jeff
    Replies:
    13
    Views:
    1,180
    vanilla
    Oct 6, 2007
  3. Emmanouil Angelakis

    Calling external program from within python

    Emmanouil Angelakis, Jul 25, 2008, in forum: Python
    Replies:
    10
    Views:
    594
    Terry Reedy
    Jul 25, 2008
  4. Eric
    Replies:
    3
    Views:
    657
  5. hrn
    Replies:
    6
    Views:
    651
    Roedy Green
    Sep 9, 2009
Loading...

Share This Page