Re: Calling external program in C++

jcoffin_at_taeus.com
Date: 12/15/04


Date: 14 Dec 2004 16:26:01 -0800


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


Relevant Pages

  • Re: Preallocate ostringstream buffer
    ... I want to write about 8Mb of data to a ostringstream and I do not ... With ostrstream I can map the stream onto a preallocated buffer, ...
    (microsoft.public.vc.stl)
  • Re: ostrstream initial size.
    ... >>ostrstream has been deprecated. ... > There is no way to reserve space in an ostringstream. ... > indeed often a manual buffer is maintained. ... allows one to easily create a stream and streambuffer that uses a ...
    (microsoft.public.vc.stl)
  • Re: Preallocate ostringstream buffer
    ... I want to write about 8Mb of data to a ostringstream and I do not ... memory as the data is written. ... With ostrstream I can map the stream onto a preallocated buffer, ...
    (microsoft.public.vc.stl)
  • Re: Preallocate ostringstream buffer
    ... I want to write about 8Mb of data to a ostringstream and I do not want the stream continually reallocating ever larger chunks of memory as the data is written. ... With ostrstream I can map the stream onto a preallocated buffer, ...
    (microsoft.public.vc.stl)