ここで SO 投稿を使用する: How do I read the results of a system() call in C++?
任意のシステム コマンドを実行し、任意の出力を文字列として返す関数を作成できました。
string pipeAndGetResults(string cmd)
{
const char* ccmd = cmd.c_str();
FILE* stream = popen( ccmd, "r" );
std::ostringstream output;
while( !feof( stream ) && !ferror( stream ))
{
char buf[128];
int bytesRead = fread( buf, 1, 128, stream );
output.write( buf, bytesRead );
}
string result = output.str();
boost::trim(result);
return result;
}
私は常にこれを、「即座に」値を生成するシステム コマンドに使用してきました。私の質問は、cmd の実行に 1 分ほど時間がかかり、結果を書き込む場合にもこの関数が機能するかどうかです。Python で同様のことを行うのに問題がありましたpexpect
。cmdに時間がかかると結果を待っている間にタイムアウトになり、cmdの実行時間を上限にできませんでした。eof
質問は、実行に時間がかかってもcmd が常に書き込みを行うかどうかに単純化されると思いますか?