2016-08-23 7 views
1

私は、計算の結果を開始して出力するためのパラメータを取得するコンソールアプリケーションを持っています。コンソールアプリケーションにデータを渡して出力を処理する方法は?

例(docsから):ステップバイステップモードそれはのような作業年代に

gdaltransform -s_srs EPSG:28992 -t_srs EPSG:31370 

  1. gdaltransform -s_srs EPSG:28992 -t_srs EPSG:31370 [Enterキーを押し]
  2. キーから入力しますボード:177502 311865 [Enterキーを押します]
  3. それは画面新しい座標上の印刷です:244296.723070577 165937.350438393 1.60975147597492

私は、Dからだそれを呼び出す入力parametrsを通過し、そしてそれは、出力のハンドルする必要があります。

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出力を読み、その後、アプリを終了するには?

答えて

2
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); 
} 
+0

「クローズ」と呼ばれる理由はありますか?たぶん 'フラッシュ'で十分でしょうか? – sigod

+0

'flush'は十分であるはずですが、私のCentOS 7ではそうしてはいけません。どうしてか分かりません。たぶん 'gdaltransform'はstdioをフラッシュしないでしょう –

関連する問題