Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > calling external program from within C++

Reply
Thread Tools

calling external program from within C++

 
 
Juggler
Guest
Posts: n/a
 
      02-24-2005
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

 
Reply With Quote
 
 
 
 
Steven
Guest
Posts: n/a
 
      02-24-2005
Did you try system("bash nameofscript arg1 arg2")?
(I think that is the syntax.)

 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      02-24-2005
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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-24-2005
"Peter Nilsson" <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      02-24-2005
Keith Thompson wrote:
> "Peter Nilsson" <(E-Mail Removed)> 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

 
Reply With Quote
 
Nils Weller
Guest
Posts: n/a
 
      02-24-2005
In article <(E-Mail Removed) .com>,
Peter Nilsson wrote:
> Keith Thompson wrote:
>> "Peter Nilsson" <(E-Mail Removed)> 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''
 
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 programs from within a Tomcat application pwaring@gmail.com Java 7 07-29-2008 11:45 AM
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
Create references to external scipt files from within an external script file Mellow Crow Javascript 6 11-04-2005 01:16 PM
Calling external program in C++ Jon Slaughter C++ 9 12-15-2004 03:07 PM



Advertisments