|
|
C++ question
|
|
|
|
Dedicated MacNNer
Join Date: Dec 2003
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Not using system(), but using popen().
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Nov 1999
Location: Rochester, MN
Status:
Offline
|
|
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;
}
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Dec 2003
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
Junior Member
Join Date: Nov 1999
Location: Rochester, MN
Status:
Offline
|
|
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?
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Dec 2003
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
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'."
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Jul 2005
Status:
Offline
|
|
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?
|
|
|
|
|
|
|
|
|
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status:
Offline
|
|
fortezza: check out the man pages for fork and execvp...
|
"Against stupidity, the gods themselves contend in vain" (Schiller)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|