calling external program from within C++

Discussion in 'C Programming' started by Juggler, Feb 24, 2005.

  1. Juggler

    Juggler Guest

    Hi,
    I have a shell script which takes three arguments, how can I call this
    script from within a C++ program.
    I am new to C++ programming and not sure how to accomplish this.
    I tried using system() call but could make it work, must be doing
    something wrong.
    Appreciate any help in this regard.

    Thanks,
    Juggler
     
    Juggler, Feb 24, 2005
    #1
    1. Advertising

  2. Juggler

    Steven Guest

    Did you try system("bash nameofscript arg1 arg2")?
    (I think that is the syntax.)
     
    Steven, Feb 24, 2005
    #2
    1. Advertising

  3. Juggler wrote:
    > I have a shell script which takes three arguments, how can I call
    > this script from within a C++ program.


    Please don't ask C++ questions in comp.lang.c.

    Try comp.lang.c++.

    --
    Peter
     
    Peter Nilsson, Feb 24, 2005
    #3
  4. "Peter Nilsson" <> writes:
    > Juggler wrote:
    >> I have a shell script which takes three arguments, how can I call
    >> this script from within a C++ program.

    >
    > Please don't ask C++ questions in comp.lang.c.
    >
    > Try comp.lang.c++.


    But the answer happens to be the same in C and C++: use the system()
    function.

    The OP says he tried this and it didn't work. Without knowing exactly
    what he tried or how it "didn't work", we can't guess what the problem
    might be.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Feb 24, 2005
    #4
  5. Keith Thompson wrote:
    > "Peter Nilsson" <> writes:
    > > Juggler wrote:
    > > > I have a shell script which takes three arguments, how can
    > > > I call this script from within a C++ program.

    ^^^^^^^^^^^^^^^^^^^^^^^^^
    > >
    > > Please don't ask C++ questions in comp.lang.c.
    > >
    > > Try comp.lang.c++.

    >
    > But the answer happens to be the same in C and C++: use the
    > system() function.


    No. The function may be the same, but the _application_ of it isn't.

    > The OP says he tried this and it didn't work. Without knowing
    > exactly what he tried or how it "didn't work", we can't guess
    > what the problem might be.


    We don't _need_ to. The OP explicitly wants a _C++_ solution. Where
    should the OP go for that? comp.lang.c?!

    But think about it, Keith...

    Since <stdlib.h> is deprecated in C++, then giving them a _C_
    solution is the _wrong_ answer, even if it happens to be
    correct in clc.

    Even if that isn't the case, do you think clc should start
    accepting code with #include <iostream>, etc... etc... in
    cases where there's a chance that there is a C solution?

    Personally, I think not, hence my reply.

    --
    Peter
     
    Peter Nilsson, Feb 24, 2005
    #5
  6. Juggler

    Nils Weller Guest

    Re: [OT] calling external program from within C++

    In article <>,
    Peter Nilsson wrote:
    > Keith Thompson wrote:
    >> "Peter Nilsson" <> writes:
    >> > Juggler wrote:
    >> > > I have a shell script which takes three arguments, how can
    >> > > I call this script from within a C++ program.

    [...]
    >> But the answer happens to be the same in C and C++: use the
    >> system() function.

    [...]
    > But think about it, Keith...
    >
    > Since <stdlib.h> is deprecated in C++, then giving them a _C_
    > solution is the _wrong_ answer, even if it happens to be
    > correct in clc.


    The fact that stdlib.h is deprecated in C++ is only of academic
    interest, if at all. The C++ standardization committee still actively
    tries to keep the language as compatible with C as possible (``... but
    no more!''), so removal of the C headers would be about the most
    nonsensical step they could take, since it would instantly break source
    compatibility with virtually every (complete) piece of C code for
    absolutely no good reason. Don't expect it to happen within the next
    decade.

    And even if they will be removed some time in a very distant future,
    then you can wait for an even longer time, or indeed forever, for the
    implementors to drop support as well (and as I'll explain below, many if
    not most typical implementations of the standard C++ library *still* do
    not even get the C++ replacements for the C headers right.)

    There are other valid reasons against using the C headers, but the
    deprecation one is certainly not one that weighs much at all, and you
    can ignore it if you feel that your C++ code should be using them (e.g.
    because you may wish to compile the code with both C and C++ compilers,
    whether that is a good thing to wish or not aside.)

    A *good* reason is that the C++ versions ought to keep the namespace
    clean by placing most of what is in the C headers into the standard
    namespace and making it available only upon explicit request. But
    unfortunately, even this one usually does not hold in practice, because
    many C++ libraries implement the C++ versions atop of the C headers, and
    they do *not* keep the namespace clean as they should.

    SuSE Linux with libstdc++-devel-3.3-23:

    /home/nils [0]> cat new.cpp
    #include <cstdio>

    typedef int FILE;

    int
    main(void) {
    }
    /home/nils [0]> c++ new.cpp -o new
    new.cpp:3: error: conflicting types for `typedef int FILE'
    /usr/include/stdio.h:46: error: previous declaration as `typedef struct
    _IO_FILE FILE'


    HP-UX 11i with HP's aC++ A.03.50:

    nils@Ariel:~> aCC new.cpp -o new -AA
    Error 173: "new.cpp", line 3 # Redefinied symbol 'FILE': previously
    defined at ["/usr/include/stdio.h", line 45]
    typedef int FILE;
    ^^^^

    (Note that the above code is actually illegal because the C++ standard
    says that:

    For each type T from the Standard C library, the types ::T and
    std::T are reserved to the implementation and, when defined, ::T
    shall be identical to std::T.

    However, I used this example only for the illustrative error messages.
    You can e.g. call printf() without explicitly prepending it with std::
    or using an equivalent ``using'' directive to test the implementation,
    as well.)

    --
    My real email address is ``nils<at>gnulinux<dot>nl''
     
    Nils Weller, Feb 24, 2005
    #6
    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. Jon Slaughter

    Calling external program in C++

    Jon Slaughter, Dec 13, 2004, in forum: C++
    Replies:
    9
    Views:
    25,937
  2. Jeff
    Replies:
    13
    Views:
    1,184
    vanilla
    Oct 6, 2007
  3. Replies:
    7
    Views:
    1,422
  4. Emmanouil Angelakis

    Calling external program from within python

    Emmanouil Angelakis, Jul 25, 2008, in forum: Python
    Replies:
    10
    Views:
    595
    Terry Reedy
    Jul 25, 2008
  5. Mellow Crow
    Replies:
    6
    Views:
    441
    Richard Cornford
    Nov 4, 2005
Loading...

Share This Page