Catching output of system()

A

Arvin Portlock

I've written a perl cgi script but it's too slow so I need
to rewrite it in C++. The perl script passes parameters to
a search engine on our local filesystem and processes the
results (number of hits, etc.). I've never had to do anything
like this in C++ before and I don't know where to start.
So what is the equivalent in C++ to something like this?

open (RESULTS, "srch -q $word |");

Which is the perl method of opening a handle on the output
of a pipe. I imagine I'd like to use system() but there doesn't
seem to be a way to capture the output of the call. Reading
through similar messages in this group I get the impression
this really can't be done. It seems like I need to pass the
search results to the program through the operating system
(the results are VERY large), i.e., through STDIN. This would
be less convenient since I'd like to package everything up
in one program.

Is this true? Is there not a way to do this?
 
L

Leor Zolman

I've written a perl cgi script but it's too slow so I need
to rewrite it in C++. The perl script passes parameters to
a search engine on our local filesystem and processes the
results (number of hits, etc.). I've never had to do anything
like this in C++ before and I don't know where to start.
So what is the equivalent in C++ to something like this?

open (RESULTS, "srch -q $word |");

Which is the perl method of opening a handle on the output
of a pipe. I imagine I'd like to use system() but there doesn't
seem to be a way to capture the output of the call. Reading
through similar messages in this group I get the impression
this really can't be done. It seems like I need to pass the
search results to the program through the operating system
(the results are VERY large), i.e., through STDIN. This would
be less convenient since I'd like to package everything up
in one program.

Is this true? Is there not a way to do this?

This is pretty platform-specific. Let me just suggest you download my
STLFilt package for MSVC (link down in my sig), and examine the source code
file CL.CPP. In it, Thomas Becker crafted some code to do piping between
Win32 processes (one of which just happens to be a Perl interpreter,
actually...). This may give you a leg-up.
Good luck,
-leor


Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
G

Gianni Mariani

Arvin said:
I've written a perl cgi script but it's too slow so I need
to rewrite it in C++. The perl script passes parameters to
a search engine on our local filesystem and processes the
results (number of hits, etc.). I've never had to do anything
like this in C++ before and I don't know where to start.
So what is the equivalent in C++ to something like this?

open (RESULTS, "srch -q $word |");

Which is the perl method of opening a handle on the output
of a pipe. I imagine I'd like to use system() but there doesn't
seem to be a way to capture the output of the call. Reading
through similar messages in this group I get the impression
this really can't be done. It seems like I need to pass the
search results to the program through the operating system
(the results are VERY large), i.e., through STDIN. This would
be less convenient since I'd like to package everything up
in one program.

Is this true? Is there not a way to do this?

There is no -standard- way of doing this. However, you can use popen
which exists in many systems (_popen in win32). If you're using a
Linux/Unix system then you can implement it as a pipe/fork call and not
use the C stdio at all.
 
D

Default User

So what is the equivalent in C++ to something like this?

open (RESULTS, "srch -q $word |");
Which is the perl method of opening a handle on the output
of a pipe.

There isn't one. You are into the platform-specific realm now. If on a
UNIX type system, the popen() function may be what you need. Questions
about that should be addressed to comp.unix.programmer. Otherwise, find
a newsgroup dedicated to your platform and ask there.
I imagine I'd like to use system() but there doesn't
seem to be a way to capture the output of the call.

That's correct.




Brian Rodenborn
 
A

Arvin Portlock

Arvin said:
I've written a perl cgi script but it's too slow so I need
to rewrite it in C++. The perl script passes parameters to
a search engine on our local filesystem and processes the
results (number of hits, etc.). I've never had to do anything
like this in C++ before and I don't know where to start.
So what is the equivalent in C++ to something like this?

open (RESULTS, "srch -q $word |");

Which is the perl method of opening a handle on the output
of a pipe. I imagine I'd like to use system() but there doesn't
seem to be a way to capture the output of the call. Reading
through similar messages in this group I get the impression
this really can't be done. It seems like I need to pass the
search results to the program through the operating system
(the results are VERY large), i.e., through STDIN. This would
be less convenient since I'd like to package everything up
in one program.

Is this true? Is there not a way to do this?
Thanks for the suggestions! I know how to do this now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top