View mode: basic / threaded / horizontal-split · Log in · Help
October 31, 2005
capturing std out
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
October 31, 2005
Re: capturing std out
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.
October 31, 2005
Re: capturing std out
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
October 31, 2005
Re: capturing std out
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!
Top | Discussion index | About this forum | D home