PHP Classes

File: example10.php

Recommend this page to a friend!
  Classes of Alessandro Rosa   CFile   example10.php   Download  
File: example10.php
Role: Example script
Content type: text/plain
Description: writing & reading an input array into a file
Class: CFile
Read and write values to binary files
Author: By
Last change:
Date: 12 years ago
Size: 2,651 bytes
 

Contents

Class file image Download
<?php // example 01 : simple open and close
     
require_once( "cfile.class.php" );
     
      echo
"<b>EXAMPLE 10</b>: writing & reading an input array into a file<br><br>" ;
     
     
$CANDIDATEfile = "example.10.txt" ;

     
$cfile = new cfile( $CANDIDATEfile, CFILE_BIG_ENDIAN );
     
$bOPEN = $cfile->open( CFILE_READWRITE_CREATE_MODE );
     
$bERR = $cfile->is_error() ;
     
      if (
$bOPEN && !$bERR ) // you can check open return value or internal error for safe operation
     
{
           echo
"OPEN FILE <b>$CANDIDATEfile</b> : SUCCESS<br>" ;
          
          
// assume such a syntax
           // left : cast the type (default value: string)
           // separator '@'
           // right : bytes number (default value: the one associated to the casted type)
          
          
$array = array( array( "STRING" => "HEADER" ),
                           array(
"DOUBLE" => 12.23 ),
                           array(
"INT" => 24 )
                         );

          
$cfile->write_array( $array );

           echo
"<pre>" ;
          
print_r( $array );
           echo
"</pre>" ;

           echo (
$cfile->close() ) ? "CLOSE FILE <b>$CANDIDATEfile</b> : SUCCESS" : $cfile->get_error_string() ;
      }
     
     
$bOPEN = $cfile->open( CFILE_READ_MODE );
     
$bERR = $cfile->is_error() ;
      if (
$bOPEN && !$bERR ) // you can check open return value or internal error for safe operation
     
{
           echo
"<br>OPEN FILE <b>$CANDIDATEfile</b> : SUCCESS<br>" ;
          
          
// anyway we shall declare the bytes number for reading a string
           // trying deleting the bytes number in the 'string' field and then see what happens

          
$array = array( array( "STRING@1" => "" ),
                           array(
"STRING@1" => "" ),
                           array(
"STRING@1" => "" ),
                           array(
"STRING@1" => "" ),
                           array(
"STRING@1" => "" ),
                           array(
"STRING@1" => "" ),
                           array(
"DOUBLE" => 0 ),
                           array(
"INT" => 0 )
                         );

          
$cfile->move_to_beginning() ;

          
$bREAD = $cfile->read_array( $array );

           if (
$bREAD )
           {
               echo
"<pre>" ;
              
print_r( $array );
               echo
"</pre>" ;
           }

           echo
"<br>" ;
           echo (
$cfile->close() ) ? "CLOSE FILE <b>$CANDIDATEfile</b> : SUCCESS" : $cfile->get_error_string() ;
      }
      else echo
$cfile->get_error_string() ;
?>