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