可以使用popen 和pclose 命令,通过管道(将其当成一个文件)从系统命令读取数据和向系统命令写入数据。
与 fopen 类似,popen 打开一个管道,以便根据第二个模式参数(“r”或“w”)来读取或写入,并返回代表管道文件控制柄。然后,可以使用标准文件功能,在管道的文件控制柄上进行读取和写入(fprint、fgetword、fgetline 等)。
如果 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 );