プロセスを実行し、終了するのを待ってから、標準出力を文字列として返す最も簡単な方法は何ですか?
Perl のバックティックのようなものです。
クロスプラットフォームのものを探していません。VC++ の最速のソリューションが必要です。
何か案は?
プロセスを実行し、終了するのを待ってから、標準出力を文字列として返す最も簡単な方法は何ですか?
Perl のバックティックのようなものです。
クロスプラットフォームのものを探していません。VC++ の最速のソリューションが必要です。
何か案は?
WinAPI ソリューション:
リダイレクトされた入力 (STARTUPINFO 構造体の hStdInput フィールド) と出力 (hStdOutput) を使用してプロセス (CreateProcess を参照) をパイプ (CreatePipe を参照) に作成し、パイプから読み取る必要があります (ReadFile を参照)。
うーん..MSDNには例としてこれがあります:
int main( void )
{
char psBuffer[128];
FILE *pPipe;
/* Run DIR so that it writes its output to a pipe. Open this
* pipe with read text attribute so that we can read it
* like a text file.
*/
if( (pPipe = _popen( "dir *.c /on /p", "rt" )) == NULL )
exit( 1 );
/* Read pipe until end of file, or an error occurs. */
while(fgets(psBuffer, 128, pPipe))
{
printf(psBuffer);
}
/* Close pipe and print return value of pPipe. */
if (feof( pPipe))
{
printf( "\nProcess returned %d\n", _pclose( pPipe ) );
}
else
{
printf( "Error: Failed to read the pipe to the end.\n");
}
}
十分に単純に思えます。C++の良さでラップするだけです。