fscanf_variation25.phpt 3.7 KB

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