User Tools

Site Tools


uphp:functions:fwrite_pack

fwrite_pack

WMMEGA FW >= 2.1179

Write data from an indexed array in binary form (packed) to a file.

Description

int fwrite_pack ( int $handle, indexed array $array , int $length )

This function performs a binary-safe write of array data. The data type is determined by the indexed array definition and can be: BYTE, SHORT, INT or FLOAT. This is an efficient way to write multiple binary bytes to a file in one go.

Parameters

$handle: Valid handle of a previously opened file

$data: The indexed array data to be written.

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

Return Values

Integer number of bytes successfully written

Examples

<?
  $fh = fopen("/fwrite_test.txt","w");
  if (!$fh) {
    print("File open failed");
  } else {
    $arr=indexed_array(4,10);  // create array of 10 floats
    for ($i=0;$i<sizeof($arr);$i++) {
      $arr[$i]=$i;
    }
    fwrite_pack($fh,$arr,sizeof($arr)); // this will write 40 bytes (10 elements * 4 bytes)
    fclose($fh);
  }
?>

See Also

pack() - Pack data

unpack() - Unpack data

fread_unpack() - Read binary data into an indexed array

uphp/functions/fwrite_pack.txt · Last modified: 2021/09/13 05:57 (external edit)