www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to execute external program and process its output?

reply Marcin Kuszczak <aarti interia.pl> writes:
Hello!

I am trying to execute external command from D program, and then process its
output. spawnvp() doesn't seem to be enough as it does not return output,
but just status as int.

How to achieve it in D (I don't know method also in C(++), so even this will
help <g>)?

---
Regards
Marcin Kuszczak
(Aarti_pl)
Jul 31 2006
next sibling parent reply Pragma <ericanderton yahoo.com> writes:
Marcin Kuszczak wrote:
 Hello!
 
 I am trying to execute external command from D program, and then process its
 output. spawnvp() doesn't seem to be enough as it does not return output,
 but just status as int.
 
 How to achieve it in D (I don't know method also in C(++), so even this will
 help <g>)?
 
 ---
 Regards
 Marcin Kuszczak
 (Aarti_pl)
Marcin, I don't know the "correct" way to do this, but perhaps I can offer a short-term hack to get you by? char[] commandline = "echo 'hello world' > output.txt"; std.process.system(commandline); writefln("result: %s",std.file.read("output.txt")); Sure its not pretty, but it does work.
Jul 31 2006
parent reply Marcin Kuszczak <aarti interia.pl> writes:
Pragma wrote:

 char[] commandline = "echo 'hello world' > output.txt";
 std.process.system(commandline);
 writefln("result: %s",std.file.read("output.txt"));
I had to modify a little bit program, but then it works - thanks a lot! -- void main() { char[] commandline = "svn info >output.txt 2>&1"; std.process.system(commandline); char[] result = cast(char[])(std.file.read("output.txt")); writefln("result: %s", result); } -- In the meantime I found also that there should be functions on C API -- #include <stdio.h> FILE *popen( const char *command, const char *type); int pclose( FILE *stream); -- but as I see it is not wrapped by D. I don't have any idea how to do it. If anyone can give some more info it would be great. BTW if D wants to be also language for scripting, it should have such a possibility in standard library. -- Regards Marcin Kuszczak (Aarti_pl)
Jul 31 2006
parent reply Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
Marcin Kuszczak wrote:
 Pragma wrote:
 
 char[] commandline = "echo 'hello world' > output.txt";
 std.process.system(commandline);
 writefln("result: %s",std.file.read("output.txt"));
I had to modify a little bit program, but then it works - thanks a lot! -- void main() { char[] commandline = "svn info >output.txt 2>&1"; std.process.system(commandline); char[] result = cast(char[])(std.file.read("output.txt")); writefln("result: %s", result); } -- In the meantime I found also that there should be functions on C API -- #include <stdio.h> FILE *popen( const char *command, const char *type); int pclose( FILE *stream); -- but as I see it is not wrapped by D. I don't have any idea how to do it. If anyone can give some more info it would be great.
extern (C) { typedef void FILE; FILE* popen(char* cmd, char* type); int pclose(FILE* stream); } should work
Jul 31 2006
parent Marcin Kuszczak <aarti interia.pl> writes:
Tom S wrote:

 Marcin Kuszczak wrote:
 Pragma wrote:
 
 char[] commandline = "echo 'hello world' > output.txt";
 std.process.system(commandline);
 writefln("result: %s",std.file.read("output.txt"));
I had to modify a little bit program, but then it works - thanks a lot! -- void main() { char[] commandline = "svn info >output.txt 2>&1"; std.process.system(commandline); char[] result = cast(char[])(std.file.read("output.txt")); writefln("result: %s", result); } -- In the meantime I found also that there should be functions on C API -- #include <stdio.h> FILE *popen( const char *command, const char *type); int pclose( FILE *stream); -- but as I see it is not wrapped by D. I don't have any idea how to do it. If anyone can give some more info it would be great.
extern (C) { typedef void FILE; FILE* popen(char* cmd, char* type); int pclose(FILE* stream); } should work
Thanks! using C was easier than I thought... (Second method still does not work for me, but it is just a mater of time :-) ) -- Regards Marcin Kuszczak (Aarti_pl)
Jul 31 2006
prev sibling parent reply BCS <BCS pathlink.com> writes:
Marcin Kuszczak wrote:
 Hello!
 
 I am trying to execute external command from D program, and then process its
 output. spawnvp() doesn't seem to be enough as it does not return output,
 but just status as int.
 
 How to achieve it in D (I don't know method also in C(++), so even this will
 help <g>)?
 
 ---
 Regards
 Marcin Kuszczak
 (Aarti_pl)
IIRC somewhere back a few months ago, someone made a stream object that did just what you want. You could even write to the program's standard input by outputting to the stream.
Jul 31 2006
next sibling parent reply Marcin Kuszczak <aarti interia.pl> writes:
BCS wrote:

 Marcin Kuszczak wrote:
 Hello!
 
 I am trying to execute external command from D program, and then process
 its output. spawnvp() doesn't seem to be enough as it does not return
 output, but just status as int.
 
 How to achieve it in D (I don't know method also in C(++), so even this
 will help <g>)?
 
 ---
 Regards
 Marcin Kuszczak
 (Aarti_pl)
IIRC somewhere back a few months ago, someone made a stream object that did just what you want. You could even write to the program's standard input by outputting to the stream.
How to find it? is it somewere on Wiki? -- Regards Marcin Kuszczak (Aarti_pl)
Jul 31 2006
parent BCS <BCS pathlink.com> writes:
Marcin Kuszczak wrote:
 BCS wrote:
 
 
Marcin Kuszczak wrote:

Hello!

I am trying to execute external command from D program, and then process
its output. spawnvp() doesn't seem to be enough as it does not return
output, but just status as int.

How to achieve it in D (I don't know method also in C(++), so even this
will help <g>)?

---
Regards
Marcin Kuszczak
(Aarti_pl)
IIRC somewhere back a few months ago, someone made a stream object that did just what you want. You could even write to the program's standard input by outputting to the stream.
How to find it? is it somewere on Wiki?
These look somewhat like what I remember. http://www.digitalmars.com/d/archives/digitalmars/D/29556.html http://www.digitalmars.com/d/archives/digitalmars/D/learn/542.html
Jul 31 2006
prev sibling parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Mon, 31 Jul 2006 15:53:00 -0700, BCS <BCS pathlink.com> wrote:
 Marcin Kuszczak wrote:
 Hello!
  I am trying to execute external command from D program, and then  
 process its
 output. spawnvp() doesn't seem to be enough as it does not return  
 output,
 but just status as int.
  How to achieve it in D (I don't know method also in C(++), so even  
 this will
 help <g>)?
  ---
 Regards
 Marcin Kuszczak
 (Aarti_pl)
IIRC somewhere back a few months ago, someone made a stream object that did just what you want. You could even write to the program's standard input by outputting to the stream.
Oh, me, me, pick me! Attached are the classes I wrote back then :) Let me know if you have any trouble. Regan
Jul 31 2006
parent Marcin Kuszczak <aarti interia.pl> writes:
Regan Heath wrote:

 On Mon, 31 Jul 2006 15:53:00 -0700, BCS <BCS pathlink.com> wrote:
 Marcin Kuszczak wrote:
 Hello!
  I am trying to execute external command from D program, and then
 process its
 output. spawnvp() doesn't seem to be enough as it does not return
 output,
 but just status as int.
  How to achieve it in D (I don't know method also in C(++), so even
 this will
 help <g>)?
  ---
 Regards
 Marcin Kuszczak
 (Aarti_pl)
IIRC somewhere back a few months ago, someone made a stream object that did just what you want. You could even write to the program's standard input by outputting to the stream.
Oh, me, me, pick me! Attached are the classes I wrote back then :) Let me know if you have any trouble. Regan
Thanks! I will give it a try. It would be awesome if such a class could find its way into the standard library... -- Regards Marcin Kuszczak (Aarti_pl)
Aug 01 2006