Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > C++ question

C++ question
Thread Tools
KraziKid
Dedicated MacNNer
Join Date: Dec 2003
Status: Offline
Reply With Quote
Jun 22, 2005, 05:22 AM
 
I was wondering if it is possible for my C++ program to read the output of a system call. For instance, say I use the stdlib function system() to call "ls ~/" how can I read this in to a string, or an array, or anything? I need to keep track of the output of some other programs. Is there any other way to do this without using system?
15 inch MacBook Pro 2.16 GHz, 2 GB RAM, 7200 RPM 100GB HDD.

Dual 2.5 GHz Power Mac G5, 1 GB RAM, 250 GB HDD, ATI Radeon X800XT.
     
Angus_D
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Jun 22, 2005, 01:04 PM
 
Not using system(), but using popen().
     
samm
Junior Member
Join Date: Nov 1999
Location: Rochester, MN
Status: Offline
Reply With Quote
Jun 22, 2005, 01:32 PM
 
here is some quick C code using popen:

Code:
#include <stdio.h> #include <stdlib.h> int main() { FILE* foo = popen("ls -l", "r"); if (foo == NULL) return -1; char fooBuffer[128]; while(!feof(foo)) { if (fgets(fooBuffer, 128, foo) != NULL) { printf( "%s", fooBuffer); } } pclose(foo); return 0; }
     
KraziKid  (op)
Dedicated MacNNer
Join Date: Dec 2003
Status: Offline
Reply With Quote
Jun 22, 2005, 02:45 PM
 
Originally Posted by samm
here is some quick C code using popen:

Code:
#include <stdio.h> #include <stdlib.h> int main() { FILE* foo = popen("ls -l", "r"); if (foo == NULL) return -1; char fooBuffer[128]; while(!feof(foo)) { if (fgets(fooBuffer, 128, foo) != NULL) { printf( "%s", fooBuffer); } } pclose(foo); return 0; }
Thanks for the help. Is there any way to do this using C++ and streams?
15 inch MacBook Pro 2.16 GHz, 2 GB RAM, 7200 RPM 100GB HDD.

Dual 2.5 GHz Power Mac G5, 1 GB RAM, 250 GB HDD, ATI Radeon X800XT.
     
samm
Junior Member
Join Date: Nov 1999
Location: Rochester, MN
Status: Offline
Reply With Quote
Jun 22, 2005, 02:58 PM
 
Originally Posted by KraziKid
Thanks for the help. Is there any way to do this using C++ and streams?
There is no portable way to convert the FILE pointer to a C++ istream. There are some gnu extensions to g++ that may do this, but I am not sure. The C code I posted above should work just fine in a C++ compiler, why do you want to use streams?
     
KraziKid  (op)
Dedicated MacNNer
Join Date: Dec 2003
Status: Offline
Reply With Quote
Jun 22, 2005, 03:04 PM
 
Originally Posted by samm
There is no portable way to convert the FILE pointer to a C++ istream. There are some gnu extensions to g++ that may do this, but I am not sure. The C code I posted above should work just fine in a C++ compiler, why do you want to use streams?
I'm more comfortable working in C++ code rather than a mixture of C and C++. I do not know "pure" C.

I am also only going to be working with Darwin based systems for this.
( Last edited by KraziKid; Jun 22, 2005 at 03:10 PM. )
15 inch MacBook Pro 2.16 GHz, 2 GB RAM, 7200 RPM 100GB HDD.

Dual 2.5 GHz Power Mac G5, 1 GB RAM, 250 GB HDD, ATI Radeon X800XT.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jun 22, 2005, 03:06 PM
 
Originally Posted by KraziKid
Thanks for the help. Is there any way to do this using C++ and streams?
This seems to be what you want.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Angus_D
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Jun 22, 2005, 06:21 PM
 
Originally Posted by KraziKid
I'm more comfortable working in C++ code rather than a mixture of C and C++. I do not know "pure" C.
If you don't know "pure" C, then you don't know C++, period. C++ is a superset of C.
     
fortezza
Fresh-Faced Recruit
Join Date: Jul 2005
Status: Offline
Reply With Quote
Jul 24, 2005, 04:09 PM
 
What if you want to start a process and then exit immediately without terminating the process you started? Say you have a program that checks to make sure important system processes are running, starts any that are not running, and then exits?
     
parallax
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Jul 25, 2005, 01:05 AM
 
fortezza: check out the man pages for fork and execvp...
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
   
 
Forum Links
Forum 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 On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 05:42 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,