如何将数据传递给控制台应用程序并处理它的输出?
How to pass data to console app and handle it's output?
我有一个控制台应用程序,它获取参数以启动,然后输出计算结果。
示例(来自 docs):
gdaltransform -s_srs EPSG:28992 -t_srs EPSG:31370
177502 311865
在逐步模式下,它的工作方式如下:
gdaltransform -s_srs EPSG:28992 -t_srs EPSG:31370
[按回车键]
- 我从键盘输入:
177502 311865
[按回车键]
- 它在屏幕上打印新坐标:
244296.723070577 165937.350438393 1.60975147597492
我需要从 D 调用它,传递输入参数,并处理它的输出。
好像需要用到pipeShell,但是不知道怎么用。
这是我的代码:
import std.stdio;
import std.process;
import std.file;
void main()
{
auto pipes = pipeProcess(["gdaltransform", " -s_srs epsg:4326 -t_srs EPSG:3857"], Redirect.stdout);
scope(exit) wait(pipes.pid);
}
但是如何读取 gdaltransform
输出到变量然后终止应用程序?
import std.stdio;
import std.process;
void main() {
// spawn child process by pipeProcess
auto pipes = pipeProcess(["gdaltransform", "-s_srs", "EPSG:28992", "-t_srs", "EPSG:31370"], Redirect.all);
// or by pipeShell
//auto pipes = pipeShell("gdaltransform -s_srs EPSG:28992 -t_srs EPSG:31370", Redirect.all);
scope(exit) {
// send terminate signal to the child process
kill(pipes.pid);
// waiting for terminate
wait(pipes.pid);
}
// write data to child's stdin
pipes.stdin.writeln("177502, 311865");
// close child's stdin
pipes.stdin.close();
// read data from child's stdout
string line = pipes.stdout.readln();
write("result: ", line);
}
我有一个控制台应用程序,它获取参数以启动,然后输出计算结果。
示例(来自 docs):
gdaltransform -s_srs EPSG:28992 -t_srs EPSG:31370
177502 311865
在逐步模式下,它的工作方式如下:
gdaltransform -s_srs EPSG:28992 -t_srs EPSG:31370
[按回车键]- 我从键盘输入:
177502 311865
[按回车键] - 它在屏幕上打印新坐标:
244296.723070577 165937.350438393 1.60975147597492
我需要从 D 调用它,传递输入参数,并处理它的输出。
好像需要用到pipeShell,但是不知道怎么用。
这是我的代码:
import std.stdio;
import std.process;
import std.file;
void main()
{
auto pipes = pipeProcess(["gdaltransform", " -s_srs epsg:4326 -t_srs EPSG:3857"], Redirect.stdout);
scope(exit) wait(pipes.pid);
}
但是如何读取 gdaltransform
输出到变量然后终止应用程序?
import std.stdio;
import std.process;
void main() {
// spawn child process by pipeProcess
auto pipes = pipeProcess(["gdaltransform", "-s_srs", "EPSG:28992", "-t_srs", "EPSG:31370"], Redirect.all);
// or by pipeShell
//auto pipes = pipeShell("gdaltransform -s_srs EPSG:28992 -t_srs EPSG:31370", Redirect.all);
scope(exit) {
// send terminate signal to the child process
kill(pipes.pid);
// waiting for terminate
wait(pipes.pid);
}
// write data to child's stdin
pipes.stdin.writeln("177502, 311865");
// close child's stdin
pipes.stdin.close();
// read data from child's stdout
string line = pipes.stdout.readln();
write("result: ", line);
}