1--TEST-- 2Test fscanf() function: usage variations - integer formats with resource 3--FILE-- 4<?php 5 6/* 7 Prototype: mixed fscanf ( resource $handle, string $format [, mixed &$...] ); 8 Description: Parses input from a file according to a format 9*/ 10 11/* Test fscanf() to scan resource type using different integer format types */ 12 13$file_path = dirname(__FILE__); 14 15echo "*** Test fscanf(): different integer format types with resource ***\n"; 16 17// create a file 18$filename = "$file_path/fscanf_variation4.tmp"; 19$file_handle = fopen($filename, "w"); 20if($file_handle == false) 21 exit("Error:failed to open file $filename"); 22 23// non-integer type of data 24 25// resource type variable 26$fp = fopen (__FILE__, "r"); 27$dfp = opendir ( dirname(__FILE__) ); 28 29// array of resource types 30$resource_types = array ( 31 $fp, 32 $dfp 33); 34 35$int_formats = array( "%d", "%hd", "%ld", "%Ld", " %d", "%d ", "% d", "\t%d", "\n%d", "%4d", "%30d", "%[0-9]", "%*d"); 36 37$counter = 1; 38 39// writing to the file 40foreach($resource_types as $value) { 41 @fprintf($file_handle, $value); 42 @fprintf($file_handle, "\n"); 43} 44// closing the file 45fclose($file_handle); 46 47// opening the file for reading 48$file_handle = fopen($filename, "r"); 49if($file_handle == false) { 50 exit("Error:failed to open file $filename"); 51} 52 53$counter = 1; 54// reading the values from file using different integer formats 55foreach($int_formats as $int_format) { 56 // rewind the file so that for every foreach iteration the file pointer starts from bof 57 rewind($file_handle); 58 echo "\n-- iteration $counter --\n"; 59 while( !feof($file_handle) ) { 60 var_dump( fscanf($file_handle,$int_format) ); 61 } 62 $counter++; 63} 64 65// closing the resources 66fclose($fp); 67closedir($dfp); 68 69echo "\n*** Done ***"; 70?> 71--CLEAN-- 72<?php 73$file_path = dirname(__FILE__); 74$filename = "$file_path/fscanf_variation4.tmp"; 75unlink($filename); 76?> 77--EXPECTF-- 78*** Test fscanf(): different integer format types with resource *** 79 80-- iteration 1 -- 81array(1) { 82 [0]=> 83 NULL 84} 85array(1) { 86 [0]=> 87 NULL 88} 89bool(false) 90 91-- iteration 2 -- 92array(1) { 93 [0]=> 94 NULL 95} 96array(1) { 97 [0]=> 98 NULL 99} 100bool(false) 101 102-- iteration 3 -- 103array(1) { 104 [0]=> 105 NULL 106} 107array(1) { 108 [0]=> 109 NULL 110} 111bool(false) 112 113-- iteration 4 -- 114array(1) { 115 [0]=> 116 NULL 117} 118array(1) { 119 [0]=> 120 NULL 121} 122bool(false) 123 124-- iteration 5 -- 125array(1) { 126 [0]=> 127 NULL 128} 129array(1) { 130 [0]=> 131 NULL 132} 133bool(false) 134 135-- iteration 6 -- 136array(1) { 137 [0]=> 138 NULL 139} 140array(1) { 141 [0]=> 142 NULL 143} 144bool(false) 145 146-- iteration 7 -- 147 148Warning: fscanf(): Bad scan conversion character " " in %s on line %d 149NULL 150 151Warning: fscanf(): Bad scan conversion character " " in %s on line %d 152NULL 153bool(false) 154 155-- iteration 8 -- 156array(1) { 157 [0]=> 158 NULL 159} 160array(1) { 161 [0]=> 162 NULL 163} 164bool(false) 165 166-- iteration 9 -- 167array(1) { 168 [0]=> 169 NULL 170} 171array(1) { 172 [0]=> 173 NULL 174} 175bool(false) 176 177-- iteration 10 -- 178array(1) { 179 [0]=> 180 NULL 181} 182array(1) { 183 [0]=> 184 NULL 185} 186bool(false) 187 188-- iteration 11 -- 189array(1) { 190 [0]=> 191 NULL 192} 193array(1) { 194 [0]=> 195 NULL 196} 197bool(false) 198 199-- iteration 12 -- 200array(1) { 201 [0]=> 202 NULL 203} 204array(1) { 205 [0]=> 206 NULL 207} 208bool(false) 209 210-- iteration 13 -- 211array(0) { 212} 213array(0) { 214} 215bool(false) 216 217*** Done *** 218