Go to: Return value. Related commands. Examples.

Synopsis

popen <string> <string>

Like fopen, popen opens a file for reading or writing depending on the second mode argument ("r" or "w"). The command is executed using the system function call and a pipe is created from that command's input or output. If popen returns 0 something went wrong. The standard file functions for reading and writing to/from a file may be used on the pipe opened by popen (fprint, fgetword, fgetline etc).

Return value

int

Related commands

system, pclose, feof, fgetline, fgetword, fopen, fclose, fflush, fprint, fread, frewind, fwrite

Examples

  
  // 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 );