fread_unpack

WMMEGA FW >= 2.1179

Read data from a file into an indexed array in binary form.

Description

int fread_unpack ( int $handle, string $format , int $count , int $interval )

This function performs a binary-safe read of data from a file into an indexed array that is automatically created. The format parameter determines the element size of the array.

Parameters

$handle: Valid handle of a previously opened file

$format: String containing one of the following items:

Value Description
l signed long (always 32 bit, machine byte order)
f floating point(always 32 bit, machine byte order)
c signed byte

$count: Number of elements to read (this will be multiplied by the array element size in byte which could be 1,2 or 4 depending on the data type)

$interval: Interval in elements to skip between reads (keep this to 1 to read a block of data)

Return Values

array indexed array containing the data

Examples

<?
  // assuming your file contains the following float:
  // 1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0
  $fh = fopen("/data.dat","r");
  if (!$fh) {
    print("File open failed");
  } else {
    $arr=fread_unpack($fh,'f',10,1); 
    // this will contain an array of 10 floating points read contiguously
    print_r($arr); 
    // 1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10
 
    $arr=fread_unpack($fh,'f',10,2); 
    // this will contain an array of 10 floating point values spaced at 2 floats apart.
 
    print_r($arr); 
    // 1.0,3.0,5.0,7.0,9.0,11.0,13.0,15.0,17.0,19.0
 
    fclose($fh);
  }
?>

See Also

pack() - Pack data

unpack() - Unpack data

fwrite_pack() - Write binary data from an indexed array