fscanf_variation19.phpt 3.5 KB

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