1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
--TEST--
Test fscanf() function: error conditions
--FILE--
<?php
echo "*** Testing fscanf() for error conditions ***\n";
$file_path = __DIR__;
$filename = "$file_path/fscanf_error.tmp";
$file_handle = fopen($filename, 'w');
if ($file_handle == false)
exit("Error:failed to open file $filename");
fwrite($file_handle, "hello world");
fclose($file_handle);
// invalid file handle
try {
fscanf($file_handle, "%s");
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
// number of formats in format strings not matching the no of variables
$file_handle = fopen($filename, 'r');
if ($file_handle == false)
exit("Error:failed to open file $filename");
try {
fscanf($file_handle, "%d%s%f", $int_var, $string_var);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
fclose($file_handle);
// different invalid format strings
$invalid_formats = array("", "%", "%h", "%.", "%d%m");
// looping to use various invalid formats with fscanf()
foreach($invalid_formats as $format) {
$file_handle = fopen($filename, 'r');
if ($file_handle == false)
exit("Error:failed to open file $filename");
try {
var_dump(fscanf($file_handle, $format));
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}
fclose($file_handle);
}
echo "\n*** Done ***";
?>
--CLEAN--
<?php
$file_path = __DIR__;
$filename = "$file_path/fscanf_error.tmp";
unlink($filename);
?>
--EXPECT--
*** Testing fscanf() for error conditions ***
fscanf(): supplied resource is not a valid File-Handle resource
Different numbers of variable names and field specifiers
array(0) {
}
Bad scan conversion character "
Bad scan conversion character "
Bad scan conversion character "."
Bad scan conversion character "m"
*** Done ***
|