system command

N

NewYorker

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.
 
V

Victor Bazarov

NewYorker said:
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
 
J

Jack Klein

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.
 
E

Emanuel Ziegler

Victor said:
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::eek:pstream does the same with output streams.

HTH
Emanuel
 
B

Buster

Emanuel said:
#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 <string> header
to avoid the fixed-size character buffer.

Regards,
Buster.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top