C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

system command

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
NewYorker
Guest





PostPosted: Thu Mar 04, 2004 3:52 am    Post subject: system command Reply with 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.




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





PostPosted: Thu Mar 04, 2004 4:08 am    Post subject: Re: system command Reply with quote



"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





PostPosted: Thu Mar 04, 2004 4:08 am    Post subject: Re: system command Reply with quote



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





PostPosted: Thu Mar 04, 2004 10:57 am    Post subject: Re: system command Reply with quote

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





PostPosted: Thu Mar 04, 2004 12:46 pm    Post subject: Re: system command Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.