2016-08-10 3 views
3

this answerで説明したように、私はWindowsコマンドを実行し、Visual StudioプロジェクトでC++内の出力を取得しようとしました。コマンドを実行して出力を取得する:popenとpclose undefined

std::string executeCommand (const char* cmd) { 
    char buffer[128]; 
    std::string result = ""; 
    std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose); 
    if (!pipe) throw std::runtime_error("popen() failed!"); 

    while (!feof(pipe.get())) { 
     if (fgets(buffer, 128, pipe.get()) != NULL) 
      result += buffer; 
    } 
    return result; 
} 

問題は、これがcompiling.The次のエラーが与えられていない、次のとおりです。

Error: identifier "popen" is undefined. 
Error: identifier "pclose" is undefined. 

答えて

3

マイクロソフトのコンパイラを使用しているので、アンダースコアがpopenpcloseの初めに必要とされています。置き換えますと

std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose); 

std::shared_ptr<FILE> pipe(_popen(cmd, "r"), _pclose); 
+0

はいくつかの標準的なされていないことを回避したファイルが含まれていますか? –

関連する問題