Calling external program in C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Slaughter

    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++?



  • Victor Bazarov

    #2
    Re: Calling external program in C++

    Jon Slaughter wrote:[color=blue]
    > is there a C++ method of calling an external executable program? I know you
    > can do it using C, but what about C++?[/color]

    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

    Comment

    • Lemon tree

      #3
      Re: Calling external program in C++

      "Jon Slaughter" <nobody@nowhere .com> ha scritto nel messaggio
      news:10rrvqecbt ia878@corp.supe rnews.com...[color=blue]
      > is there a C++ method of calling an external executable program? I know[/color]
      you[color=blue]
      > can do it using C, but what about C++?[/color]

      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



      Comment

      • Default User

        #4
        Re: Calling external program in C++

        Jon Slaughter wrote:
        [color=blue]
        > is there a C++ method of calling an external executable program? I
        > know you can do it using C, but what about C++?[/color]

        Same way, system().




        Brian

        Comment

        • Mike Wahler

          #5
          Re: Calling external program in C++


          "Lemon tree" <lemon@tree.i t> wrote in message
          news:M4ovd.2898 60$b5.14124045@ news3.tin.it...[color=blue]
          > "Jon Slaughter" <nobody@nowhere .com> ha scritto nel messaggio
          > news:10rrvqecbt ia878@corp.supe rnews.com...[color=green]
          > > is there a C++ method of calling an external executable program? I know[/color]
          > you[color=green]
          > > can do it using C, but what about C++?[/color]
          >
          > I think that there is no standard way to do this...[/color]

          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.
          [color=blue]
          >
          > So, in my opinion, the best way is to use the C -style[/color]


          'system()' is as much a part of the C++ standard library as
          it is of the C standard library.
          [color=blue]
          >possibly wrapped
          > inside a C++ method or something similar.[/color]

          Why wrap it?

          -Mike


          Comment

          • msalters

            #6
            Re: Calling external program in C++


            Mike Wahler wrote:[color=blue]
            > "Lemon tree" <lemon@tree.i t> wrote in message
            > news:M4ovd.2898 60$b5.14124045@ news3.tin.it...[color=green]
            > > "Jon Slaughter" <nobody@nowhere .com> ha scritto nel messaggio
            > > I think that there is no standard way to do this...[/color]
            >
            > 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.
            >[color=green]
            > >
            > > So, in my opinion, the best way is to use the C -style[/color]
            >
            > 'system()' is as much a part of the C++ standard library as
            > it is of the C standard library.
            >[color=green]
            > >possibly wrapped
            > > inside a C++ method or something similar.[/color]
            >
            > Why wrap it?[/color]

            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

            Comment

            • Mike Wahler

              #7
              Re: Calling external program in C++


              "msalters" <Michiel.Salter s@logicacmg.com > wrote in message
              news:1103023866 .888986.162720@ z14g2000cwz.goo glegroups.com.. .[color=blue]
              >
              > Mike Wahler wrote:[color=green]
              > > "Lemon tree" <lemon@tree.i t> wrote in message
              > > news:M4ovd.2898 60$b5.14124045@ news3.tin.it...[color=darkred]
              > > > "Jon Slaughter" <nobody@nowhere .com> ha scritto nel messaggio
              > > > I think that there is no standard way to do this...[/color]
              > >
              > > 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.
              > >[color=darkred]
              > > >
              > > > So, in my opinion, the best way is to use the C -style[/color]
              > >
              > > 'system()' is as much a part of the C++ standard library as
              > > it is of the C standard library.
              > >[color=darkred]
              > > >possibly wrapped
              > > > inside a C++ method or something similar.[/color]
              > >
              > > Why wrap it?[/color]
              >
              > Wrap it so you can add arguments more easily;[/color]

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

              [color=blue]
              > the C interface
              > of system has a horrible way to add arguments (memory fiddling).[/color]

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

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

              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


              Comment

              • jcoffin@taeus.com

                #8
                Re: Calling external program in C++

                > is there a C++ method of calling an external executable program?[color=blue]
                > I know you can do it using C, but what about C++?[/color]

                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::ostringstr eam;

                ostringstream &execute(ostrin gstream &s)
                {
                std::system(s.s tr().c_str());
                return s;
                }

                ostringstream &operator<<(ost ringstream &s,
                ostringstream &(*manip)(ostri ngstream &s))
                {
                return manip(s);
                }

                int main() {
                ostringstream x;

                x << "ls " << execute;

                return 0;
                }

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



                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.

                Comment

                • jcoffin@taeus.com

                  #9
                  Re: Calling external program in C++

                  > is there a C++ method of calling an external executable program?[color=blue]
                  > I know you can do it using C, but what about C++?[/color]

                  Here's a possibility you might find interesting:

                  #include <sstream>
                  #include <cstdlib>

                  using std::ostringstr eam;

                  ostringstream &execute(ostrin gstream &s) {
                  std::system(s.s tr().c_str());
                  return s;
                  }

                  ostringstream &operator<<(ost ringstream &s,
                  ostringstream &(*manip)(ostri ngstream &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:



                  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.

                  Comment

                  • jcoffin@taeus.com

                    #10
                    Re: Calling external program in C++

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

                    Perhaps you'll find this an interesting possibility:

                    #include <sstream>
                    #include <cstdlib>

                    using std::ostringstr eam;

                    ostringstream &execute(ostrin gstream &s) {
                    std::system(s.s tr().c_str());
                    return s;
                    }

                    ostringstream &operator<<(ost ringstream &s,
                    ostringstream &(*manip)(ostri ngstream &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.

                    Comment

                    Working...