By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,128 Members | 1,034 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,128 IT Pros & Developers. It's quick & easy.

how to call a program and get the returned data?

P: n/a
i want to write an gui (an applet for Kicker in KDE) for a shellcommand in
qt3/c++.

So i need to call an executable and get its output back into my c++ program.

what would be the correct way to implement this?
I'm glad for links and any helpful hints.

--
kind regards,

Jonas Stein <ne**@jonasstein.de>
Dec 19 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Jonas Stein <ne**@jonasstein.dewrote:
i want to write an gui (an applet for Kicker in KDE) for a shellcommand in
qt3/c++.

So i need to call an executable and get its output back into my c++ program.

what would be the correct way to implement this?
I'm glad for links and any helpful hints.
AFAIK there is no standard way to do this.

std::system() will return some implementation-specific number.

Some systems have a function called popen() which opens a pipe and
allows your program to read from it; you will need to examine your
system to see if it is available.

One way to do it that is mostly standards-compliant is to redirect the
output from the first program to a file, then open this file with your
program. Redirecting output is implementation-specific, but everything
else should be OK.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Dec 19 '06 #2

P: n/a

Marcus Kwok napsal:
Jonas Stein <ne**@jonasstein.dewrote:
i want to write an gui (an applet for Kicker in KDE) for a shellcommand in
qt3/c++.

So i need to call an executable and get its output back into my c++ program.

what would be the correct way to implement this?
I'm glad for links and any helpful hints.

AFAIK there is no standard way to do this.

std::system() will return some implementation-specific number.

Some systems have a function called popen() which opens a pipe and
allows your program to read from it; you will need to examine your
system to see if it is available.

One way to do it that is mostly standards-compliant is to redirect the
output from the first program to a file, then open this file with your
program. Redirecting output is implementation-specific, but everything
else should be OK.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Marcus is right. There is no standard way how to do it. But as long as
you are writing it for KDE, you can safely use popen, because it should
work on all POSIX systems.

Maybe is some API for executing processes also in qt (it is not only
GUI), but I am not sure. If it exists, it is exactly, what are you
searching for.

More effective way than popen should be creating new process with fork
and getting its output. I do not remmeber details, search for fork and
dup2 examples.

Dec 19 '06 #3

P: n/a
On Tue, 19 Dec 2006 22:25:35 +0100, Jonas Stein <ne**@jonasstein.de>
wrote:
>i want to write an gui (an applet for Kicker in KDE) for a shellcommand in
qt3/c++.

So i need to call an executable and get its output back into my c++ program.

what would be the correct way to implement this?
I'm glad for links and any helpful hints.
How much output are you expecting? A single int can be returned
through the return from main() if your operating system allows you
access to it.

If your program has the correct structure, you can return data in
globals accessible to all relevant processes.

You could write the data to a file in a predetermined location, and
pick it up after the gui applet has finished.

If we have more details we will be able to be more specific. You
might also want to ask in a group dedicated to your operating system
as it is possible that the solution to your problem will contain some
parts that are OS specific.

rossum

Dec 19 '06 #4

P: n/a

Ondra Holub napsal:
Marcus Kwok napsal:
Jonas Stein <ne**@jonasstein.dewrote:
i want to write an gui (an applet for Kicker in KDE) for a shellcommand in
qt3/c++.
>
So i need to call an executable and get its output back into my c++ program.
>
what would be the correct way to implement this?
I'm glad for links and any helpful hints.
AFAIK there is no standard way to do this.

std::system() will return some implementation-specific number.

Some systems have a function called popen() which opens a pipe and
allows your program to read from it; you will need to examine your
system to see if it is available.

One way to do it that is mostly standards-compliant is to redirect the
output from the first program to a file, then open this file with your
program. Redirecting output is implementation-specific, but everything
else should be OK.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply

Marcus is right. There is no standard way how to do it. But as long as
you are writing it for KDE, you can safely use popen, because it should
work on all POSIX systems.

Maybe is some API for executing processes also in qt (it is not only
GUI), but I am not sure. If it exists, it is exactly, what are you
searching for.

More effective way than popen should be creating new process with fork
and getting its output. I do not remmeber details, search for fork and
dup2 examples.
If you need only one start of cli program and only get its output, you
can start it this way:
program_without_gui | program_with_gui
And then read standard input.

Dec 19 '06 #5

P: n/a

Jonas Stein wrote:
i want to write an gui (an applet for Kicker in KDE) for a shellcommand in
qt3/c++.

So i need to call an executable and get its output back into my c++ program.

what would be the correct way to implement this?
I'm glad for links and any helpful hints.
If you need to interact with a program that reads and/or writes to the
standard io ports you need to use pipes. Basically you fork, start
your new program, and override the standard io ports for the new
program to pipes, and you connect to those pipes in the GUI and then
interact with them. It isn't all that hard, but it isn't always that
easy either. Both programs need to be written so they don't get frozen
up when buffering isn't quite what is expected. You'll need to make
sure to flush all lines in the console program for instance and never
leave anything partially read.

There is no C++ standard way to do it but there are standard ways to do
it. Most systems implement posix. Windows is likely to of course be
different.

I have a project that does it in C++ using Qt on the GUI side and
standard C++ on the other: http://xiangqi-engine.sourceforge.net

Dec 19 '06 #6

P: n/a
Some systems have a function called popen() which opens a pipe and
allows your program to read from it; you will need to examine your
system to see if it is available.
Thanks to all who have answered my question so fast!
I will use popen() and pclose()
Since i know the function-name i can find a lot of documentation easily.

Thanks again.

--
kind regards,

Jonas Stein <ne**@jonasstein.de>
Dec 19 '06 #7

P: n/a
Jonas Stein a écrit :
>Some systems have a function called popen() which opens a pipe and
allows your program to read from it; you will need to examine your
system to see if it is available.

Thanks to all who have answered my question so fast!
I will use popen() and pclose()
Since i know the function-name i can find a lot of documentation easily.
You can try pstream on sourceforge:
http://pstreams.sourceforge.net/

It is a neat C++ wrapping around popen.

Michael
Dec 20 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.