システム コマンド パイプの読み書きを行う
 
 
 

popenpclose コマンドを使用して、パイプを通してシステム コマンドに(ファイルと同じように)データの読み書きを行うことができます。

fopen と同様に、popen は第 2 引数(「r」または「w」)に従って読み込みまたは書き込み用のパイプを開き、パイプを表すファイル ハンドルを返します。その後、パイプのファイル ハンドルに関する読み込みおよび書き込み用の標準的なファイル関数を使用することができます(fprintfgetwordfgetline など)。

popen が 0 を返す場合、システム コマンドに不都合が生じています。

例:

// Unix specific example. Note: if you really want to get a directory
// listing, please use the "getFileList" command instead. This is just
// used as a simple example.
//
$pipe = popen( "ls -1", "r" );
string $dirList[];
while ( !feof( $pipe ) ) {
 $dirList[size( $dirList )] = fgetline( $pipe );
}
pclose( $pipe );
// Windows specific example. Note: if you really want to get a directory
// listing, please use the "getFileList" command instead. This is just
// used as a simple example.
//
$pipe = popen( "DIR /B", "r" );
string $dirList[];
while ( !feof( $pipe ) ) {
 $dirList[size( $dirList )] = fgetline( $pipe );
}
pclose( $pipe );