LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-11-2007, 12:50 AM   #1
prongs_386
LQ Newbie
 
Registered: Jul 2007
Posts: 11

Rep: Reputation: 0
c++ system return


Hi,
just wondering whether it is possible to get the feedback from console from something called within the system() command.
eg.
system("service dhcpd status");
would print into console whether the dhcpd service is running, however this information isnt returned to the program as far as i'm aware.

Is it possible to retrieve this output?

Thanks
 
Old 07-11-2007, 07:53 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
There are several ways, depending on your taste.

First, if you want the output to go to the console and to the program, you can do this:

Code:
system("service dhcpd status | tee tempfile.txt");
and then read file tempfile.txt in your program.

Another way would be to do I/O redirection by hand, using fork(), exec(), and other calls. Effectively, your program would do much of what bash or other shells do. To get started, go here:

http://www.gnu.org/software/libc/man...scriptors.html
http://www.gnu.org/software/libc/man...Launching-Jobs

These pages are intended for C users, but C++ users will find them interesting also.

Hope this helps.
 
Old 07-11-2007, 08:10 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
'system' inherits standard input/output/error from the calling process. Try 'popen' instead of 'system'. If not, you will need to 'pipe', 'fork', 'dup2', then 'system'. There is more to it than that, so I think 'popen' is the way to go.
ta0kira
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
system() and return values PatrickNew Programming 1 02-07-2007 04:35 AM
does a system call return value?? docetes Programming 2 03-16-2006 05:40 PM
Is it possible to use system() and get the return value from the system call newguy21 Programming 1 08-11-2004 01:37 PM
How to use return values from system functions? kpachopoulos Programming 2 07-30-2004 03:09 AM
How can get values return from a system call quenn Programming 1 09-16-2003 11:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration