<?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() ;
?>
|