fscanfPARSES input from a FILE according to a format.
Undue substitutions will produce unpredictable results.
This function is similar to sscanf, but it takes its input from a file associated with handle and interprets the input according to the specified format, which is described in the documentation for sprintf.
Any whitespace in the format string matches any whitespace in the input stream.
This means that even a tab, ( \t ) in the format string can match a single space character in the input stream.
If only two parameters were passed to this function, the values parsed will be returned as an array.
Otherwise, if optional parameters are passed, the function will return the number of assigned values.
<?php
mix fscanf ( res $handle , str $format [,
mix &$arg1, ..., mix $argN ] )
where,
$handle = File system pointer
( As typically created by fopen )
$format = STRING FORMAT, with internal,
% replaceable specifiers
( SEE the below TABLE )
$arg1, ... , $argN = SUBSTITUTE ARGUMENTS
( Passed by reference )
?>
handle
The file pointer, typically created by fopen.
$format
Each SPECIFIER, present in FORMATTED STRING, must have its own SUBSTITUTE ARGUMENT.
SPECIFIER % |
TYPE |
ARGUMENT |
COMMENT |
%% |
|
literally % |
|
%b |
INTEGER |
Represented as BINARY |
|
%c |
INTEGER |
Represented as CHARACTER - ASCII |
|
%d |
INTEGER |
Represented as DECIMAL NUMBER |
+/- |
%e |
FLOAT |
SCIENTIFIC NOTATION |
|
%E |
FLOAT |
As %e with the use of UPPERCASE
|
|
%u |
INTEGER |
Presented as an UNSIGNED DECIMAL number. |
no signal |
%f |
FLOAT |
Compatible with LOCALE |
|
%F |
FLOAT |
As %f not compatible with LOCALE |
|
%g |
FLOAT |
Shorter of %e and %f. |
|
%G |
FLOAT |
Shorter of %E e %F |
|
%o |
INTEGER |
Presented as an OCTAL number |
|
%s |
STRING |
STRING
|
|
%x |
INTEGER |
Presented as an HEXADECIMAL number |
lowercase |
%X |
INTEGER |
Presented as an HEXADECIMAL number |
UPERCASE |
ADDITIONAL FORMATS |
0 |
ZERO |
FILLING WITH ZERO |
Number of desired decimal places |
" " |
SPACE |
FILLING WITH SPACES |
Number of desired places |
- |
HYPHEN |
Left-aligned value |
Right-aligned as default |
N1.N2 |
N1 |
INTEGER
|
NUMBER OF DIGITS |
N2 |
DECIMAL |
NUMBER OF DIGITS |
ed48 |
&$arg1 ... &$argN
Parsed arguments, passed by reference.
EXERCISE
<?php
$file01 = __DIR__ . '/browser.txt';
$browsr01 = $_SERVER['HTTP_USER_AGENT'];
// This is an internal variable
$fp = fopen($file01, 'w');
if($fp == TRUE)
{
fprintf($fp, '%s', $browsr01);
fclose($fp);
echo 'The file WAS SAVED as expected.<br><br>';
echo file_get_contents($file01) . '<br><br>';
}
else
{
echo 'The file WAS NOT SAVED as expected.<br><br>';
}
$file01 = __DIR__ . '/browser.txt';
$fp = fopen($file01, 'r');
$flctt01 = file_get_contents($file01);
$arrxpl01 = explode(" ", $flctt01);
for ($x=0; $x < count($arrxpl01); $x++)
{
echo $arrxpl01[$x] . '<br>';
}
fscanf($fp, "%s %s %s %s %s %s %s %s %s %s %s %s",
$g1, $g2, $g3, $g4, $g5, $g6, $g7,
$g8, $g9, $g10, $g11, $g12);
echo '<br><br>' . $g11;
fclose($fp);
unlink($file01);
?>
EXERCISE
<?php
$filename = __DIR__ . "/fscanf.dat";
file_put_contents($filename, "data");
$fp = fopen($filename, "rt");
var_dump(fscanf($fp, "%d", $v));
var_dump($v);
fclose($fp);
$fp = fopen($filename, "rt");
var_dump(fscanf($fp, "%s", $v));
var_dump($v);
fclose($fp);
$fp = fopen($filename, "rt");
try {
fscanf($fp, "%s", $v, $v1);
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
var_dump($v);
var_dump($v1);
fclose($fp);
$v = array();
$v1 = array();
$fp = fopen($filename, "rt");
try {
fscanf($fp, "", $v, $v1);
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
var_dump($v);
var_dump($v1);
fclose($fp);
$v = array();
$v1 = array();
$fp = fopen($filename, "rt");
try {
fscanf($fp, "%.a", $v, $v1);
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
var_dump($v);
var_dump($v1);
fclose($fp);
@unlink($filename);
touch($filename);
$fp = fopen($filename, "rt");
var_dump(fscanf($fp, "%s", $v));
var_dump($v);
fclose($fp);
file_put_contents($filename, "data");
$fp = fopen($filename, "rt");
try {
var_dump(fscanf($fp, "%s%d", $v));
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() for its return type */
$file_path = __DIR__;
echo "Testing fscanf():<br>
for its return type without
third argument.<br><br>";
// create a file
$filename = "$file_path/fscanf03.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
@fwrite($file_handle, "hello_world ");
@fwrite($file_handle, 12345);
fclose($file_handle);
// open file for reading
$file_handle = fopen($filename, "r");
// capturing the return value from
// fscanf() called without third argument
$return_value = fscanf($file_handle, "%s");
var_dump( is_array($return_value), $return_value);
// return type is an array
fclose($file_handle);
$cont= file_get_contents($filename);
echo "<br><br>File contents:<br>$cont";
$filename = "$file_path/fscanf03.tmp";
unlink($filename);
?>
EXERCISE
<?php
$file_path = __DIR__;
echo "Test fscanf():<br>
different integer format types
with different integer values.<br>";
// create a file
$filename = "$file_path/fscanf04.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// different valid integer values
$valid_ints = array(
0,
1,
-1,
-2147483648, // max negative integer value
-2147483647,
2147483647, // max positive integer value
2147483640,
0x123B, // integer as hexadecimal
0x12ab,
0Xfff,
0XFA,
-0x80000000, // max negative integer as hexadecimal
0x7fffffff, // max positive integer as hexadecimal
0x7FFFFFFF, // max positive integer as hexadecimal
0123, // integer as octal
01, // should be quivalent to octal 1
-020000000000, // max negative integer as octal
017777777777 // max positive integer as octal
);
// various integer formats
$int_formats = array( "%d", "%hd", "%ld", "%Ld", " %d",
"%d ", "% d", "\t%d", "\n%d",
"%4d", "%30d", "%[0-9]", "%*d");
$counter = 1;
// writing to the file
foreach($valid_ints as $int_value) {
@fprintf($file_handle, $int_value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using different integer formats
foreach($int_formats as $int_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$int_format));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
}
$counter++;
}
$file_path = __DIR__;
$filename = "$file_path/fscanf04.tmp";
$cont= file_get_contents($filename);
echo "<br><br><br>File contents:<br>$cont";
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan strings
* using different integer format types
*/
$file_path = __DIR__;
echo "Test fscanf():<br>
different integer format types with strings.<br><br>";
// create a file
$filename = "$file_path/fscanf05.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array of strings
$strings = array (
"",
'',
"0",
'0',
"1",
'1',
"\x01",
'\x01',
"\01",
'\01',
'string',
"string",
"true",
"FALSE",
'false',
'TRUE',
"NULL",
'null'
);
$int_formats = array( "%d", "%hd", "%ld", "%Ld",
" %d", "%d ", "% d", "\t%d", "\n%d",
"%4d", "%30d", "%[0-9]", "%*d");
$counter = 1;
// writing to the file
foreach($strings as $string) {
@fprintf($file_handle, $string);
@fprintf($file_handle, "<br>");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different integer formats
foreach($int_formats as $int_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$int_format));
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan boolean data
* using different integer format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different integer format types
with boolean data.<br>";
// create a file
$filename = "$file_path/fscanf06.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array of boolean types
$bool_types = [
true,
false,
TRUE,
FALSE,
];
$int_formats = [ "%d", "%hd", "%ld", "%Ld",
" %d", "%d ", "% d", "\t%d", "\n%d",
"%4d", "%30d", "%[0-9]", "%*d" ];
$counter = 1;
// writing to the file
foreach($bool_types as $value) {
@fprintf($file_handle, $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different integer formats
foreach($int_formats as $int_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$int_format));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan boolean data
using different integer format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different integer format types with boolean data.<br>";
// create a file
$filename = "$file_path/fscanf07.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array of boolean types
$bool_types = array (
true,
false,
TRUE,
FALSE,
);
$int_formats = array( "%d", "%hd", "%ld",
"%Ld", " %d", "%d ", "% d", "\t%d",
"\n%d", "%4d", "%30d", "%[0-9]", "%*d");
$counter = 1;
// writing to the file
foreach($bool_types as $value) {
@fprintf($file_handle, $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different integer formats
foreach($int_formats as $int_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$int_format));
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan different float
values using different format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different float format types
with different float values.<br>";
// create a file
$filename = "$file_path/fscanf08.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// different valid float values
$valid_floats = array(
-2147483649, // float value
2147483648, // float value
-0x80000001, // float value, beyond max negative int
0x800000001, // float value, beyond max positive int
020000000001, // float value, beyond max positive int
-020000000001, // float value, beyond max negative int
0.0,
-0.1,
10.0000000000000000005,
10.5e+5,
1e5,
-1e5,
1e-5,
-1e-5,
1e+5,
-1e+5,
1E5,
-1E5,
1E+5,
-1E+5,
1E-5,
-1E-5,
.5e+7,
-.5e+7,
.6e-19,
-.6e-19,
.05E+44,
-.05E+44,
.0034E-30,
-.0034E-30
);
// various float formats
$float_formats = array( "%f",
"%hf", "%lf", "%Lf",
" %f", "%f ", "% f",
"\t%f", "\n%f", "%4f",
"%30f", "%[0-9]", "%*f",
);
$counter = 1;
// writing to the file
foreach($valid_floats as $float_value) {
@fprintf($file_handle, $float_value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different float formats
foreach($float_formats as $float_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$float_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan resource type
using different float format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different float format types with resource.<br>";
// create a file
$filename = "$file_path/fscanf09.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// resource type variable
$fp = fopen (__FILE__, "r");
$dfp = opendir ( __DIR__ );
// array of resource types
$resource_types = [
$fp,
$dfp
];
$float_formats = [ "%f",
"%hf", "%lf", "%Lf",
" %f", "%f ", "% f",
"\t%f", "\n%f", "%4f",
"%30f", "%[0-9]", "%*f"
];
$counter = 1;
// writing to the file
foreach($resource_types as $value) {
@fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different formats formats
foreach($float_formats as $float_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump( fscanf($file_handle,$float_format) );
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
// closing the resources
fclose($fp);
closedir($dfp);
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan arrays using
different float format types */
$file_path = __DIR__;
echo "Test fscanf():<br:
different float format types with arrays.<br>";
// create a file
$filename = "$file_path/fscanf10.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array types
$array_types = array (
array(),
array(0),
array(1),
array(NULL),
array(null),
array("string"),
array(true),
array(TRUE),
array(false),
array(FALSE),
array(1,2,3,4),
array(1 => "One", "two" => 2)
);
$float_formats = array( "%f",
"%hf", "%lf", "%Lf",
" %f", "%f ", "% f",
"\t%f", "\n%f", "%4f",
"%30f", "%[0-9]", "%*f"
);
$counter = 1;
// writing to the file
foreach($array_types as $value) {
@fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different float formats
foreach($float_formats as $float_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br<";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$float_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan strings using
different float format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different float format types with strings.<br>";
// create a file
$filename = "$file_path/fscanf11.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array of strings
$strings = array (
"",
'',
"0",
'0',
"1",
'1',
"\x01",
'\x01',
"\01",
'\01',
'string',
"string",
"true",
"FALSE",
'false',
'TRUE',
"NULL",
'null'
);
$float_formats = array( "%f", "%hf", "%lf", "%Lf",
" %f", "%f ", "% f", "\t%f",
"\n%f", "%4f", "%30f", "%[0-9]", "%*f");
$counter = 1;
// writing to the file
foreach($strings as $string) {
@fprintf($file_handle, $string);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different float formats
foreach($float_formats as $float_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$float_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan boolean data
using different float format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different float format types with boolean data.<br>";
// create a file
$filename = "$file_path/fscanf12.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array of boolean types
$bool_types = [
true,
false,
TRUE,
FALSE,
];
$float_formats = [ "%f", "%hf", "%lf", "%Lf",
" %f", "%f ", "% f", "\t%f",
"\n%f", "%4f", "%30f", "%[0-9]", "%*f" ];
$counter = 1;
// writing to the file
foreach($bool_types as $value) {
@fprintf($file_handle, $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different float formats
foreach($float_formats as $float_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$float_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan different strings
using different string format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different string format types
with different string.<br>";
// create a file
$filename = "$file_path/fscanf13.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// different valid strings
/* string created using Heredoc (<<<) */
$heredoc_string = <<<EOT
This is string defined
using heredoc.
EOT;
/* heredoc string with only numerics */
$heredoc_numeric_string = <<<EOT
123456 3993
4849 string
EOT;
/* null heardoc string */
$heredoc_empty_string = <<<EOT
EOT;
$heredoc_null_string = <<<EOT
NULL
EOT;
$valid_strings = array(
"",
" ",
'',
' ',
"string",
'string',
"NULL",
'null',
"FALSE",
'true',
"\x0b",
"\0",
'\0',
'\060',
"\070",
"0x55F",
"055",
"@#$#$%%$^^$%^%^$^&",
$heredoc_string,
$heredoc_numeric_string,
$heredoc_empty_string,
$heredoc_null_string
);
// various string formats
$string_formats = array( "%s",
"%hs", "%ls", "%Ls",
" %s", "%s ", "% s",
"\t%s", "\n%s", "%4s",
"%30s", "%[a-zA-Z0-9]", "%*s"
);
$counter = 1;
// writing to the file
foreach($valid_strings as $string) {
try {
fprintf($file_handle, $string);
} catch (\ValueError $e) {
} catch (\ArgumentCountError $e) {
}
fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different string formats
foreach($string_formats as $string_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$string_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan float values
using different string format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different string format types with float values.<br>";
// create a file
$filename = "$file_path/fscanf14.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array of float type values
$float_values = array (
-2147483649,
2147483648,
-0x80000001, // float value, beyond max negative int
0x800000001, // float value, beyond max positive int
020000000001, // float value, beyond max positive int
-020000000001, // float value, beyond max negative int
0.0,
-0.1,
1.0,
1e5,
-1e6,
1E8,
-1E9,
10.0000000000000000005,
10.5e+5
);
$string_formats = array( "%s",
"%hs", "%ls", "%Ls",
" %s", "%s ", "% s",
"\t%s", "\n%s", "%4s",
"%30s", "%[a-zA-Z0-9]", "%*s");
$counter = 1;
// writing to the file
foreach($float_values as $value) {
@fprintf($file_handle, $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different string formats
foreach($string_formats as $string_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$string_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan resource
type using different string format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different string format
types with resource.<br>";
// create a file
$filename = "$file_path/fscanf15.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// resource type variable
$fp = fopen (__FILE__, "r");
$dfp = opendir ( __DIR__ );
// array of resource types
$resource_types = array (
$fp,
$dfp
);
$string_formats = array( "%s",
"%hs", "%ls", "%Ls",
" %s", "%s ", "% s",
"\t%s", "\n%s", "%4s",
"%30s", "%[a-zA-Z0-9]", "%*s");
$counter = 1;
// writing to the file
foreach($resource_types as $value) {
@fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different string formats
foreach($string_formats as $string_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$string_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
// closing the resources
fclose($fp);
closedir($dfp);
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan arrays using
different string format types */
$file_path = __DIR__;
echo "*Test fscanf():<br>
different string format types with arrays.<br>";
// create a file
$filename = "$file_path/fscanf16.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array types
$array_types = [
[],
[0],
[1],
[NULL],
[null],
["string"],
[true],
[TRUE],
[false],
[FALSE],
[1,2,3,4],
[1 => "One", "two" => 2]
];
$string_formats = [ "%s",
"%hs", "%ls", "%Ls",
" %s", "%s ", "% s",
"\t%s", "\n%s", "%4s",
"%30s", "%[a-zA-Z0-9]", "%*s" ];
$counter = 1;
// writing to the file
foreach($array_types as $value) {
@fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different string formats
foreach($string_formats as $string_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$string_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan integer values
using different string format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different string format types with integer values.<br>";
// create a file
$filename = "$file_path/fscanf17.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array of string type values
$integer_values = array (
0,
1,
-1,
-2147483648, // max negative integer value
-2147483647,
2147483647, // max positive integer value
2147483640,
0x123B, // integer as hexadecimal
0x12ab,
0Xfff,
0XFA,
-0x80000000, // max negative integer as hexadecimal
0x7fffffff, // max positive integer as hexadecimal
0x7FFFFFFF, // max positive integer as hexadecimal
0123, // integer as octal
01, // should be quivalent to octal 1
-020000000000, // max negative integer as octal
017777777777 // max positive integer as octal
);
$string_formats = array( "%s",
"%hs", "%ls", "%Ls",
" %s", "%s ", "% s",
"\t%s", "\n%s", "%4s",
"%30s", "%[a-zA-Z0-9]", "%*s"
);
$counter = 1;
// writing to the file
foreach($integer_values as $value) {
@fprintf($file_handle, $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different string formats
foreach($string_formats as $string_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$string_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan boolean data
using different string format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different string format types with boolean data.<br>";
// create a file
$filename = "$file_path/fscanf18.txt";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array of boolean types
$bool_types = array (
true,
false,
TRUE,
FALSE,
);
$string_formats = array( "%s",
"%hs", "%ls", "%Ls",
" %s", "%s ", "% s",
"\t%s", "\n%s", "%4s",
"%30s", "%[a-zA-Z0-9]", "%*s");
$counter = 1;
// writing to the file
foreach($bool_types as $value) {
@fprintf($file_handle, $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different string formats
foreach($string_formats as $string_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$string_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan different integer
values using different char format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different char format types
with different integer values.<br>";
// create a file
$filename = "$file_path/fscanf19.txt";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// different valid integer values
$valid_ints = array(
0,
1,
-1,
-2147483648, // max negative integer value
-2147483647,
2147483647, // max positive integer value
2147483640,
0x123B, // integer as hexadecimal
0x12ab,
0Xfff,
0XFA,
-0x80000000, // max negative integer as hexadecimal
0x7fffffff, // max positive integer as hexadecimal
0x7FFFFFFF, // max positive integer as hexadecimal
0123, // integer as octal
01, // should be quivalent to octal 1
-020000000000, // max negative integer as octal
017777777777 // max positive integer as octal
);
// various char formats
$char_formats = array( "%c",
"%hc", "%lc", "%Lc",
" %c", "%c ", "% c",
"\t%c", "\n%c", "%4c",
"%30c", "%[a-bA-B@#$&]", "%*c");
$counter = 1;
// writing to the file
foreach($valid_ints as $int_value) {
@fprintf($file_handle, $int_value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different char formats
foreach($char_formats as $char_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$char_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan float values using
different char format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different char format types with float values.<br>";
// create a file
$filename = "$file_path/fscanf20.txt";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array of float type values
$float_values = array (
-2147483649,
2147483648,
-0x80000001, // float value, beyond max negative int
0x800000001, // float value, beyond max positive int
020000000001, // float value, beyond max positive int
-020000000001, // float value, beyond max negative int
0.0,
-0.1,
1.0,
1e5,
-1e6,
1E8,
-1E9,
10.0000000000000000005,
10.5e+5
);
$char_formats = array( "%c",
"%hc", "%lc", "%Lc",
" %c", "%c ", "% c",
"\t%c", "\n%c", "%4c",
"%30c", "%[a-zA-Z@#$&0-9]", "%*c");
$counter = 1;
// writing to the file
foreach($float_values as $value) {
@fprintf($file_handle, $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different char formats
foreach($char_formats as $char_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$char_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan resource type
using different char format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different char format types with resource.<br>";
// create a file
$filename = "$file_path/fscanf21.txt";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// resource type variable
$fp = fopen (__FILE__, "r");
$dfp = opendir ( __DIR__ );
// array of resource types
$resource_types = array (
$fp,
$dfp
);
$char_formats = array( "%c",
"%hc", "%lc", "%Lc",
" %c", "%c ", "% c",
"\t%c", "\n%c", "%4c",
"%30c", "%[a-zA-Z@#$&0-9]", "%*c");
$counter = 1;
// writing to the file
foreach($resource_types as $value) {
@fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different char formats
foreach($char_formats as $char_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$char_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
// closing the resources
fclose($fp);
closedir($dfp);
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan arrays using
different char format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different char format types with arrays.<br>";
// create a file
$filename = "$file_path/fscanf22.txt";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array types
$array_types = array (
array(),
array(0),
array(1),
array(NULL),
array(null),
array("string"),
array(true),
array(TRUE),
array(false),
array(FALSE),
array(1,2,3,4),
array(1 => "One", "two" => 2)
);
$char_formats = array( "%c",
"%hc", "%lc", "%Lc",
" %c", "%c ", "% c",
"\t%c", "\n%c", "%4c",
"%30c", "%[a-zA-Z@#$&0-9]", "%*c");
$counter = 1;
// writing to the file
foreach($array_types as $value) {
@fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different char formats
foreach($char_formats as $char_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$char_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan strings using
different char format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different char format types with strings.<br>";
// create a file
$filename = "$file_path/fscanf23.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array of strings
$strings = array (
"",
'',
"0",
'0',
"1",
'1',
"\x01",
'\x01',
"\01",
'\01',
'string',
"string",
"true",
"FALSE",
'false',
'TRUE',
"NULL",
'null'
);
$char_formats = array( "%c",
"%hc", "%lc", "%Lc",
" %c", "%c ", "% c",
"\t%c", "\n%c", "%4c",
"%30c", "%[a-zA-Z@#$&0-9]", "%*c");
$counter = 1;
// writing to the file
foreach($strings as $string) {
@fprintf($file_handle, $string);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different char formats
foreach($char_formats as $char_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$char_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan boolean data
using different char format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different char format types
with boolean data.<br>";
// create a file
$filename = "$file_path/fscanf24.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array of boolean types
$bool_types = array (
true,
false,
TRUE,
FALSE,
);
$char_formats = array( "%c",
"%hc", "%lc", "%Lc",
" %c", "%c ", "% c",
"\t%c", "\n%c", "%4c",
"%30c", "%[a-zA-Z@#$&0-9]", "%*c");
$counter = 1;
// writing to the file
foreach($bool_types as $value) {
@fprintf($file_handle, $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file
// using different char formats
foreach($char_formats as $char_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$char_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan different chars
using different char format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different char format types with chars.<br>";
// create a file
$filename = "$file_path/fscanf25.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array of chars
$char_types = array( 'a', "a", 67, -67, 99 );
$char_formats = array( "%c",
"%hc", "%lc", "%Lc",
" %c", "%c ", "% c",
"\t%c", "\n%c", "%4c",
"%30c", "%[a-zA-Z@#$&0-9]", "%*c");
$counter = 1;
// writing to the file
foreach($char_types as $char) {
@fprintf($file_handle, $char);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different char formats
foreach($char_formats as $char_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$char_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan different integer
values using different octal format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different octal format types
with different integer values.<br>";
// create a file
$filename = "$file_path/fscanf26.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// different valid integer values
$valid_ints = array(
0,
1,
-1,
-2147483648, // max negative integer value
-2147483647,
2147483647, // max positive integer value
2147483640,
0x123B, // integer as hexadecimal
0x12ab,
0Xfff,
0XFA,
-0x80000000, // max negative integer as hexadecimal
0x7fffffff, // max positive integer as hexadecimal
0x7FFFFFFF, // max positive integer as hexadecimal
0123, // integer as octal
01, // should be quivalent to octal 1
-020000000000, // max negative integer as octal
017777777777 // max positive integer as octal
);
// various octal formats
$octal_formats = array( "%o",
"%ho", "%lo", "%Lo",
" %o", "%o ", "% o",
"\t%o", "\n%o", "%4o",
"%30o", "%[0-7]", "%*o"
);
$counter = 1;
// writing to the file
foreach($valid_ints as $int_value) {
@fprintf($file_handle, $int_value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different octal formats
foreach($octal_formats as $octal_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$octal_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan float values
using different octal format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different octal format types
with float values.<br>";
// create a file
$filename = "$file_path/fscanf27.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array of float type values
$float_values = array (
-2147483649,
2147483648,
-0x80000001, // float value, beyond max negative int
0x800000001, // float value, beyond max positive int
020000000001, // float value, beyond max positive int
-020000000001, // float value, beyond max negative int
0.0,
-0.1,
1.0,
1e5,
-1e6,
1E8,
-1E9,
10.0000000000000000005,
10.5e+5
);
$octal_formats = array( "%o",
"%ho", "%lo", "%Lo",
" %o", "%o ", "% o",
"\t%o", "\n%o", "%4o",
"%30o", "%[0-7]", "%*o"
);
$counter = 1;
// writing to the file
foreach($float_values as $value) {
@fprintf($file_handle, $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different octal formats
foreach($octal_formats as $octal_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$octal_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan resource type using
different octal format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different octal format types
with resource.<br>";
// create a file
$filename = "$file_path/fscanf28.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// resource type variable
$fp = fopen (__FILE__, "r");
$dfp = opendir ( __DIR__ );
// array of resource types
$resource_types = array (
$fp,
$dfp
);
$octal_formats = array( "%o",
"%ho", "%lo", "%Lo",
" %o", "%o ", "% o",
"\t%o", "\n%o", "%4o",
"%30o", "%[0-7]", "%*o"
);
$counter = 1;
// writing to the file
foreach($resource_types as $value) {
@fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different octal formats
foreach($octal_formats as $octal_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$octal_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
}
$counter++;
}
// closing the resources
fclose($fp);
closedir($dfp);
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan arrays using
different octal format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different octal format types
with arrays.<br>";
// create a file
$filename = "$file_path/fscanf29.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array types
$array_types = array (
array(),
array(0),
array(1),
array(NULL),
array(null),
array("string"),
array(true),
array(TRUE),
array(false),
array(FALSE),
array(1,2,3,4),
array(1 => "One", "two" => 2)
);
$octal_formats = array ( "%o",
"%ho", "%lo", "%Lo",
" %o", "%o ", "% o",
"\t%o", "\n%o", "%4o",
"%30o", "%[0-7]", "%*o"
);
$counter = 1;
// writing to the file
foreach($array_types as $value) {
@fprintf($file_handle, "%s", $value);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different octal formats
foreach($octal_formats as $octal_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$octal_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to scan strings using
different hexa format types */
$file_path = __DIR__;
echo "Test fscanf():<br>
different hexa format types with strings.<br>";
// create a file
$filename = "$file_path/fscanf30.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
// array of strings
$strings = array (
"",
'',
"0",
'0',
"1",
'1',
"\x01",
'\x01',
"\01",
'\01',
'string',
"string",
"true",
"FALSE",
'false',
'TRUE',
"NULL",
'null'
);
$hexa_formats = array( "%x", "%hx", "%lx", "%Lx",
" %x", "%x ", "% x", "\t%x", "\n%x",
"%4x", "%30x", "%[0-9]", "%*x");
$counter = 1;
// writing to the file
foreach($strings as $string) {
@fprintf($file_handle, $string);
@fprintf($file_handle, "\n");
}
// closing the file
fclose($file_handle);
// opening the file for reading
$file_handle = fopen($filename, "r");
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
$counter = 1;
// reading the values from file using
// different hexa formats
foreach($hexa_formats as $hexa_format) {
// rewind the file so that for every foreach
// iteration the file pointer starts from bof
rewind($file_handle);
echo "<br><br>Iteration: $counter<br>";
while( !feof($file_handle) ) {
try {
var_dump(fscanf($file_handle,$hexa_format));
echo '<br>';
} catch (ValueError $exception) {
echo $exception->getMessage() . "<br>";
}
}
$counter++;
}
unlink($filename);
?>
EXERCISE
<?php
/* Test fscanf() to read a file when
file pointer is pointing to EOF */
$file_path = __DIR__;
echo "Test fscanf():<br>
to read a file when file pointer is pointing to EOF.<br>";
// various formats
$formats = array( "%d", "%f", "%e", "%u", " %s", "%x", "%o");
$counter = 1;
// various read modes
$modes = array("r", "rb", "rt", "r+", "r+b", "r+t",
"w+", "w+b", "w+t",
"a+", "a+b", "a+t"
);
$counter = 1;
// reading the values from file using
// different integer formats
foreach($modes as $mode) {
// create an empty file
$filename = "$file_path/fscanf31.tmp";
$file_handle = fopen($filename, "w");
if($file_handle == false)
exit("Error:failed to open file $filename");
//writing data to the file
@fwrite($file_handle, "Sample text\n");
// writing a blank line
@fwrite($file_handle, "\n");
//closing the file
fclose($file_handle);
// opening file in $mode mode
$file_handle = fopen($filename, $mode);
if($file_handle == false) {
exit("Error:failed to open file $filename");
}
echo "<br><br>Iteration: $counter<br>";
// current location
var_dump( ftell($file_handle) );
// set the file pointer to eof
var_dump( fseek($file_handle, 0, SEEK_END) );
// current location
var_dump( ftell($file_handle) );
foreach($formats as $format) {
var_dump( fscanf($file_handle,$format) );
echo '<br>';
}
$counter++;
fclose($file_handle);
unlink($filename);
}
if(file_exists($filename)) {
unlink($filename);
}
?>