1--TEST-- 2Test fscanf() function: usage variations - file pointer pointing to EOF 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 read a file when file pointer is pointing to EOF */ 12 13$file_path = dirname(__FILE__); 14 15echo "*** Test fscanf(): to read a file when file pointer is pointing to EOF ***\n"; 16 17// various formats 18$formats = array( "%d", "%f", "%e", "%u", " %s", "%x", "%o"); 19 20$counter = 1; 21 22// various read modes 23$modes = array("r", "rb", "rt", "r+", "r+b", "r+t", 24 "w+", "w+b", "w+t", 25 "a+", "a+b", "a+t" 26 ); 27 28$counter = 1; 29// reading the values from file using different integer formats 30foreach($modes as $mode) { 31 32 // create an empty file 33 $filename = "$file_path/fscanf_variation53.tmp"; 34 $file_handle = fopen($filename, "w"); 35 if($file_handle == false) 36 exit("Error:failed to open file $filename"); 37 38 //writing data to the file 39 @fwrite($file_handle, "Sample text\n"); 40 41 // writing a blank line 42 @fwrite($file_handle, "\n"); 43 44 //closing the file 45 fclose($file_handle); 46 47 // opening file in $mode mode 48 $file_handle = fopen($filename, $mode); 49 if($file_handle == false) { 50 exit("Error:failed to open file $filename"); 51 } 52 echo "\n-- iteration $counter --\n"; 53 54 // current location 55 var_dump( ftell($file_handle) ); 56 57 // set the file pointer to eof 58 var_dump( fseek($file_handle, 0, SEEK_END) ); 59 60 // current location 61 var_dump( ftell($file_handle) ); 62 63 foreach($formats as $format) { 64 var_dump( fscanf($file_handle,$format) ); 65 } 66 $counter++; 67 fclose($file_handle); 68 unlink($filename); 69} 70 71echo "\n*** Done ***"; 72?> 73--CLEAN-- 74<?php 75$file_path = dirname(__FILE__); 76$filename = "$file_path/fscanf_variation53.tmp"; 77if(file_exists($filename)) { 78 unlink($filename); 79} 80?> 81--EXPECT-- 82*** Test fscanf(): to read a file when file pointer is pointing to EOF *** 83 84-- iteration 1 -- 85int(0) 86int(0) 87int(13) 88bool(false) 89bool(false) 90bool(false) 91bool(false) 92bool(false) 93bool(false) 94bool(false) 95 96-- iteration 2 -- 97int(0) 98int(0) 99int(13) 100bool(false) 101bool(false) 102bool(false) 103bool(false) 104bool(false) 105bool(false) 106bool(false) 107 108-- iteration 3 -- 109int(0) 110int(0) 111int(13) 112bool(false) 113bool(false) 114bool(false) 115bool(false) 116bool(false) 117bool(false) 118bool(false) 119 120-- iteration 4 -- 121int(0) 122int(0) 123int(13) 124bool(false) 125bool(false) 126bool(false) 127bool(false) 128bool(false) 129bool(false) 130bool(false) 131 132-- iteration 5 -- 133int(0) 134int(0) 135int(13) 136bool(false) 137bool(false) 138bool(false) 139bool(false) 140bool(false) 141bool(false) 142bool(false) 143 144-- iteration 6 -- 145int(0) 146int(0) 147int(13) 148bool(false) 149bool(false) 150bool(false) 151bool(false) 152bool(false) 153bool(false) 154bool(false) 155 156-- iteration 7 -- 157int(0) 158int(0) 159int(0) 160bool(false) 161bool(false) 162bool(false) 163bool(false) 164bool(false) 165bool(false) 166bool(false) 167 168-- iteration 8 -- 169int(0) 170int(0) 171int(0) 172bool(false) 173bool(false) 174bool(false) 175bool(false) 176bool(false) 177bool(false) 178bool(false) 179 180-- iteration 9 -- 181int(0) 182int(0) 183int(0) 184bool(false) 185bool(false) 186bool(false) 187bool(false) 188bool(false) 189bool(false) 190bool(false) 191 192-- iteration 10 -- 193int(0) 194int(0) 195int(13) 196bool(false) 197bool(false) 198bool(false) 199bool(false) 200bool(false) 201bool(false) 202bool(false) 203 204-- iteration 11 -- 205int(0) 206int(0) 207int(13) 208bool(false) 209bool(false) 210bool(false) 211bool(false) 212bool(false) 213bool(false) 214bool(false) 215 216-- iteration 12 -- 217int(0) 218int(0) 219int(13) 220bool(false) 221bool(false) 222bool(false) 223bool(false) 224bool(false) 225bool(false) 226bool(false) 227 228*** Done *** 229