www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - capturing std out

reply Charles <Charles_member pathlink.com> writes:
Is there any way to capture stdout from an exec/system call into the program ?

In perl I'd do this:

my  output = `ls`; 
print  output;

This above allows me to parse/manipulate the output of the ls command.

How would I do the equiv in D ? 
The following executes the ls command but doesn't give me the output inside the
program.
import std.process;
char * cmd = "/bin/ls"
system(cmd);

any hlp apprec,
Charles
Oct 31 2005
parent reply zwang <nehzgnaw gmail.com> writes:
Charles wrote:
 Is there any way to capture stdout from an exec/system call into the program ?
 
 In perl I'd do this:
 
 my  output = `ls`; 
 print  output;
 
 This above allows me to parse/manipulate the output of the ls command.
 
 How would I do the equiv in D ? 
 The following executes the ls command but doesn't give me the output inside the
 program.
 import std.process;
 char * cmd = "/bin/ls"
 system(cmd);
 
 any hlp apprec,
 Charles
 
 
As far as I know, you have to call platform-specific API to do this in D. In Windows, related functions include CreatePipe, SetHandleInformation, CreateProcess, PeekNamedPipe, WaitForSingleObject, ReadFile, and CloseHandle.
Oct 31 2005
parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Mon, 31 Oct 2005 22:14:59 +0800, zwang <nehzgnaw gmail.com> wrote:
 Charles wrote:
 Is there any way to capture stdout from an exec/system call into the  
 program ?
  In perl I'd do this:
  my  output = `ls`; print  output;
  This above allows me to parse/manipulate the output of the ls command.
  How would I do the equiv in D ? The following executes the ls command  
 but doesn't give me the output inside the
 program.
 import std.process;
 char * cmd = "/bin/ls"
 system(cmd);
  any hlp apprec,
 Charles
As far as I know, you have to call platform-specific API to do this in D. In Windows, related functions include CreatePipe, SetHandleInformation, CreateProcess, PeekNamedPipe, WaitForSingleObject, ReadFile, and CloseHandle.
Yep. Here are some examples, they may even work ;) Regan
Oct 31 2005
parent Georg Wrede <georg.wrede nospam.org> writes:
Regan Heath wrote:
 On Mon, 31 Oct 2005 22:14:59 +0800, zwang <nehzgnaw gmail.com> wrote:
 
 Charles wrote:

 Is there any way to capture stdout from an exec/system call into the  
 program ?
  In perl I'd do this:
  my  output = `ls`; print  output;
  This above allows me to parse/manipulate the output of the ls command.
  How would I do the equiv in D ? The following executes the ls 
 command  but doesn't give me the output inside the
 program.
 import std.process;
 char * cmd = "/bin/ls"
 system(cmd);
  any hlp apprec,
 Charles
For debugging or quick-and-dirty purposes one might also do something like this: system("ls -l /root /home >/tmp/lsout 2>/tmp/lserr "); Whereafter you can the read the files into your program. This has the advantage of separating error and standard output from each other. For example, on my machine the files end up containing: lsout: /home: total 4 drwxr-xr-x 24 georg georg 4096 Nov 1 00:34 georg and lserr: ls: /root: Permission denied
 As far as I know, you have to call platform-specific API to do this 
 in  D. In Windows, related functions include CreatePipe,  
 SetHandleInformation, CreateProcess, PeekNamedPipe, 
 WaitForSingleObject,  ReadFile, and CloseHandle.
Yep. Here are some examples, they may even work ;)
Cool!
Oct 31 2005