 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
NewYorker Guest
|
Posted: Thu Mar 04, 2004 3:52 am Post subject: system command |
|
|
Hi,
I'm looking for code to do the following. Bascially, "System" shell out and
execute the command and return the stdout in result. You know a link OR
know the trick, please let me know.
// result contains the stdout of the command
char* result=System("ls -l");
char* result=System("cat myFile.txt");
TIA.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Mar 04, 2004 4:08 am Post subject: Re: system command |
|
|
"NewYorker" <newyorker777 (AT) hotmail (DOT) com> wrote...
| Quote: | I'm looking for code to do the following. Bascially, "System" shell out
and
execute the command and return the stdout in result. You know a link OR
know the trick, please let me know.
// result contains the stdout of the command
char* result=System("ls -l");
char* result=System("cat myFile.txt");
|
No, that is not possible in Standard C++. You need POSIX pipes to do
what you want. Off-topic here, sorry.
V
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Thu Mar 04, 2004 4:08 am Post subject: Re: system command |
|
|
On Wed, 3 Mar 2004 21:52:52 -0600, "NewYorker"
<newyorker777 (AT) hotmail (DOT) com> wrote in comp.lang.c++:
| Quote: | Hi,
I'm looking for code to do the following. Bascially, "System" shell out and
execute the command and return the stdout in result. You know a link OR
know the trick, please let me know.
// result contains the stdout of the command
char* result=System("ls -l");
char* result=System("cat myFile.txt");
TIA.
|
There is no way to do this in the standard C++ language. Typically
you build a command line that includes your operating system's output
redirection option to send the stdout of a program to a file. Then
you can open and read the file.
Your compiler and operating system combination might offer extensions
for doing this differently, but you would have to ask about in a group
supporting that combination. We don't do extensions here.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
|
|
| Back to top |
|
 |
Emanuel Ziegler Guest
|
Posted: Thu Mar 04, 2004 10:57 am Post subject: Re: system command |
|
|
Victor Bazarov wrote:
| Quote: | "NewYorker" <newyorker777 (AT) hotmail (DOT) com> wrote...
You know a link OR know the trick, please let me know.
No, that is not possible in Standard C++. You need POSIX pipes to do
what you want. Off-topic here, sorry.
|
ACK
Try http://pstreams.sourceforge.net/
There you can get a header file, that defines stream classes for piping. It
works on linux machines without problems. You simply define something like
--- source begins here ---
#include <iostream>
#include "pstream.h"
using namespace std;
int main () {
redi::ipstream foo("ls -l");
char buffer[100];
while ( foo ) {
foo.getline(buffer,100);
cout << "Redi: " << buffer << endl;
}
}
--- source begins here ---
This program simply puts the word "Redi: " in front of every line.
You can access foo exactly like cin or any ifstream object. The class
redi::opstream does the same with output streams.
HTH
Emanuel
|
|
| Back to top |
|
 |
Buster Guest
|
Posted: Thu Mar 04, 2004 12:46 pm Post subject: Re: system command |
|
|
Emanuel Ziegler wrote:
| Quote: | #include <iostream
#include "pstream.h"
using namespace std;
int main () {
redi::ipstream foo("ls -l");
char buffer[100];
while ( foo ) {
foo.getline(buffer,100);
cout << "Redi: " << buffer << endl;
}
}
|
See the FAQ for an explanation of why this code will output the final
line twice. I would also use getline from the standard
to avoid the fixed-size character buffer.
Regards,
Buster.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|