<?php // example 01 : simple open and close
require_once( "cfile.class.php" );
echo "<b>EXAMPLE 02</b>: creating a file, then we write in a double and an integer values<br><br>" ;
$CANDIDATEfile = "cfile.test.example02.xxx" ;
$cfile = new cfile( $CANDIDATEfile );
$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>" ;
echo "FILE SIZE.".( filesize( $CANDIDATEfile ) )."<br>" ;
$BINARY = 111113.1234422 ;
$nbytes = CFILE_SIZE_OF_DOUBLE ;
echo "Writing a double (<b>$BINARY</b> : $nbytes bytes) in binary mode ..<br>" ;
$bWRITE = $cfile->write( $BINARY, $nbytes, CFILE_BINARY_DOUBLE_MODE ) ;
echo ( $bWRITE ) ? "WRITE something in binary mode ...<br>" : "CAN'T WRITE in binary mode ...<br>" ;
$BINARY = 45334343 ;
$nbytes = CFILE_SIZE_OF_INT ;
echo "Writing an integer (<b>$BINARY</b> : $nbytes bytes) in binary mode ..<br>" ;
$bWRITE = $cfile->write( $BINARY, $nbytes, CFILE_BINARY_INT_MODE ) ;
echo ( $bWRITE ) ? "WRITE something in binary mode ...<br>" : "CAN'T WRITE in binary mode ...<br>" ;
$cfile->move_to_beginning();
// and read the next bytes
$nbytes = CFILE_SIZE_OF_DOUBLE ;
echo "Now trying to read some bytes ($nbytes)<br>" ;
$READ = $cfile->read( $nbytes, CFILE_BINARY_DOUBLE_MODE ) ;
echo ( $READ === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "This is what I read in $nbytes byte".( ( $nbytes == 1 ) ? "" : "s" )." : <b>$READ</b><br>" ;
// and read the next bytes
$nbytes = CFILE_SIZE_OF_INT ;
echo "Now trying to read some bytes ($nbytes)<br>" ;
$READ = $cfile->read( $nbytes, CFILE_BINARY_INT_MODE ) ;
echo ( $READ === false ) ? "CAN'T READ from file ... <b>".$cfile->get_error_string()."</b><br>" : "This is what I read in $nbytes byte".( ( $nbytes == 1 ) ? "" : "s" )." : <b>$READ</b><br>" ;
echo "<br>" ;
echo ( $cfile->close() ) ? "CLOSE FILE <b>$CANDIDATEfile</b> : SUCCESS" : $cfile->get_error_string() ;
}
else echo $cfile->get_error_string() ;
?>
|