472,354 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

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

Jul 22 '05 #1
9 1882
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
Jul 22 '05 #2
"Jon Slaughter" <no****@nowhere.com> ha scritto nel messaggio
news:10*************@corp.supernews.com...
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

Jul 22 '05 #3
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
Jul 22 '05 #4

"Lemon tree" <le***@tree.it> wrote in message
news:M4**********************@news3.tin.it...
"Jon Slaughter" <no****@nowhere.com> ha scritto nel messaggio
news:10*************@corp.supernews.com...
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
Jul 22 '05 #5

Mike Wahler wrote:
"Lemon tree" <le***@tree.it> wrote in message
news:M4**********************@news3.tin.it...
"Jon Slaughter" <no****@nowhere.com> 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

Jul 22 '05 #6

"msalters" <Mi*************@logicacmg.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

Mike Wahler wrote:
"Lemon tree" <le***@tree.it> wrote in message
news:M4**********************@news3.tin.it...
"Jon Slaughter" <no****@nowhere.com> 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
Jul 22 '05 #7
> 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::ostringstream;

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.

Jul 22 '05 #8
> 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::ostringstream;

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.

Jul 22 '05 #9
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::ostringstream;

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.

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: chandan | last post by:
Is it possible to call an external java program from a trigger in oracle 8i. I know that you can call a stored procedure in java from a trigger but i want to know is it possible to call an...
4
by: Sivakumar Shanmugam | last post by:
Group.. We are running on db2 UDF V8 on Sun solaris platform. I created an UDF which calles a C-routine(SQL_API_FN). This C-routine in turn calls an external C-function. The C-function is provided...
11
by: j23 | last post by:
I have library (static) testlib.cpp: #include <stdarg.h> void xxx(...) { char buf; va_list args; va_start(args, buf); va_end(args); }
11
by: RichN | last post by:
I am developing a c program in Visual Studio .NET 2003. I also have an Intel(R) Fortran compiler for MVS .NET My fortran sourcecode already existed. I started a new fortran project and chose to...
1
by: notregister | last post by:
How do i call out an external program(e.g. abc.exe) while hiding my current window form. when i exit this external program, how do i re-show my window form?
18
by: utab | last post by:
Dear all, I am making a system call to the well known Gnuplot with system("gnuplot"); gnuplot opens if I only supply this command but I would like to pipe that command line in my C++...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
2
by: Maydogg6 | last post by:
I need a hand with some stubborn link errors. I'm trying to recreate and old program from 6.0 into .NET, but for some reason when I try to compile I'm getting linking errors for all my function...
13
by: Jeff | last post by:
We have an intranet website that currently uses ActiveX but we need to make it cross-browser compatible and also get around the problems we've been having with making it work with IE7 and Vista. We...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.