User Tools

Site Tools


uphp:functions:fread_unpack

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

uphp:functions:fread_unpack [2021/09/13 05:57] (current)
Line 1: Line 1:
 +======fread_unpack======
 +
 +<badge>WMMEGA FW >= 2.1179</badge>
 +
 +Read data from a file into an indexed array in binary form.
 +
 +====Description====
 +
 +<well size="sm"><html>
 +<span style="font-size:125%;color:green">int
 +<span style="color:black">fread_unpack (
 +<span style="color:green">int
 +<span style="color:blue">$handle<span style="color:black">,
 +<span style="color:green">string
 +<span style="color:blue">$format
 +<span style="color:black">,
 +<span style="color:green">int
 +<span style="color:blue">$count<span style="color:black">
 +<span style="color:black">,
 +<span style="color:green">int
 +<span style="color:blue">$interval<span style="color:black">
 +)
 +</html></well>
 +
 +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====
 +
 +<html><b><span style="color:blue">$handle<span style="color:black"></b></html>:  Valid handle of a previously opened file
 +
 +<html><b><span style="color:blue">$format<span style="color:black"></b>:  <b><span style="color:green">String<span style="color:black"></b> containing one of the following items:</html>
 +^ Value  ^ Description                                      ^
 +| l      | signed long (always 32 bit, machine byte order)  |
 +| f      | floating point(always 32 bit, machine byte order)  |
 +| c      | signed byte   |
 +
 +<html><b><span style="color:blue">$count<span style="color:black"></b></html>:  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)
 +
 +<html><b><span style="color:blue">$interval<span style="color:black"></b></html>:  Interval in elements to skip between reads (keep this to 1 to read a block of data)
 +
 +====Return Values====
 +
 +<html><b><span style="color:green">array<span style="color:black"></b></html> indexed array containing the data
 +
 +====Examples====
 +
 +<code php>
 +<?
 +  // 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);
 +  }
 +?>
 +</code>
 +
 +
 +
 +====See Also====
 +
 +[[pack()]] - Pack data
 +
 +[[unpack()]] - Unpack data
 +
 +[[fwrite_pack()]] - Write binary data from an indexed array
  
uphp/functions/fread_unpack.txt · Last modified: 2021/09/13 05:57 (external edit)