473,385 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

how to call a program and get the returned data?

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
7 1346
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

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

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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
54
by: Neo | last post by:
Hi Folks, I've a simple qestion related to dynamic memory allocation in C here is the code: #include <stdio.h> int main() {
5
by: Seong-Kook Shin | last post by:
Hi, I'm reading Steve's "C Programming FAQs" in book version, and have two question regarding to Q11.16 ... Also, a `return' from `main' cannot be expected to work if data local to main might be...
5
by: SStory | last post by:
Hi all, I really needed to get the icons associated with each file that I want to show in a listview. I used the follow modified code sniplets found on the internet. I have left in...
9
by: santosh | last post by:
Hello all, I've put together a small program to count the number of characters and 'words' in a text file. The minimum length of a word, (in terms of no. of characters), as well as word...
4
by: wqyuwss | last post by:
Hi, We have several core dumps in our product. These core dump can be reproduced in the same place. That is system function call std::basic_istream<char,std::char_traits<char>>::getline. The...
0
by: mix01 | last post by:
Hi, I am trying to get some VBA code working, but am preplex as to why it does not work. I would really appreciate any level of help. Many thanks, Mix01 Version of the program
5
by: tcomer | last post by:
Hello, I'm working on an application that grabs some data from the web via HttpWebRequest. I'm using a local objects method to get the data, but the problem is that my form doesn't load until...
2
by: Radz | last post by:
when the fork system call is executed, a new process is created. The original process is called the parent process whereas the new process is called the child process. The new process consists of a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.