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