fscanf_variation7.phpt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. --TEST--
  2. Test fscanf() function: usage variations - integer 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 integer format types */
  10. $file_path = dirname(__FILE__);
  11. echo "*** Test fscanf(): different integer format types with boolean data ***\n";
  12. // create a file
  13. $filename = "$file_path/fscanf_variation7.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. $int_formats = array( "%d", "%hd", "%ld", "%Ld", " %d", "%d ", "% d", "\t%d", "\n%d", "%4d", "%30d", "%[0-9]", "%*d");
  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 integer formats
  40. foreach($int_formats as $int_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. var_dump( fscanf($file_handle,$int_format) );
  46. }
  47. $counter++;
  48. }
  49. echo "\n*** Done ***";
  50. ?>
  51. --CLEAN--
  52. <?php
  53. $file_path = dirname(__FILE__);
  54. $filename = "$file_path/fscanf_variation7.tmp";
  55. unlink($filename);
  56. ?>
  57. --EXPECTF--
  58. *** Test fscanf(): different integer format types with boolean data ***
  59. -- iteration 1 --
  60. array(1) {
  61. [0]=>
  62. int(1)
  63. }
  64. NULL
  65. array(1) {
  66. [0]=>
  67. int(1)
  68. }
  69. NULL
  70. bool(false)
  71. -- iteration 2 --
  72. array(1) {
  73. [0]=>
  74. int(1)
  75. }
  76. NULL
  77. array(1) {
  78. [0]=>
  79. int(1)
  80. }
  81. NULL
  82. bool(false)
  83. -- iteration 3 --
  84. array(1) {
  85. [0]=>
  86. int(1)
  87. }
  88. NULL
  89. array(1) {
  90. [0]=>
  91. int(1)
  92. }
  93. NULL
  94. bool(false)
  95. -- iteration 4 --
  96. array(1) {
  97. [0]=>
  98. int(1)
  99. }
  100. NULL
  101. array(1) {
  102. [0]=>
  103. int(1)
  104. }
  105. NULL
  106. bool(false)
  107. -- iteration 5 --
  108. array(1) {
  109. [0]=>
  110. int(1)
  111. }
  112. NULL
  113. array(1) {
  114. [0]=>
  115. int(1)
  116. }
  117. NULL
  118. bool(false)
  119. -- iteration 6 --
  120. array(1) {
  121. [0]=>
  122. int(1)
  123. }
  124. NULL
  125. array(1) {
  126. [0]=>
  127. int(1)
  128. }
  129. NULL
  130. bool(false)
  131. -- iteration 7 --
  132. Warning: fscanf(): Bad scan conversion character " " in %s on line %d
  133. NULL
  134. Warning: fscanf(): Bad scan conversion character " " in %s on line %d
  135. NULL
  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. bool(false)
  141. -- iteration 8 --
  142. array(1) {
  143. [0]=>
  144. int(1)
  145. }
  146. NULL
  147. array(1) {
  148. [0]=>
  149. int(1)
  150. }
  151. NULL
  152. bool(false)
  153. -- iteration 9 --
  154. array(1) {
  155. [0]=>
  156. int(1)
  157. }
  158. NULL
  159. array(1) {
  160. [0]=>
  161. int(1)
  162. }
  163. NULL
  164. bool(false)
  165. -- iteration 10 --
  166. array(1) {
  167. [0]=>
  168. int(1)
  169. }
  170. NULL
  171. array(1) {
  172. [0]=>
  173. int(1)
  174. }
  175. NULL
  176. bool(false)
  177. -- iteration 11 --
  178. array(1) {
  179. [0]=>
  180. int(1)
  181. }
  182. NULL
  183. array(1) {
  184. [0]=>
  185. int(1)
  186. }
  187. NULL
  188. bool(false)
  189. -- iteration 12 --
  190. array(1) {
  191. [0]=>
  192. string(1) "1"
  193. }
  194. array(1) {
  195. [0]=>
  196. NULL
  197. }
  198. array(1) {
  199. [0]=>
  200. string(1) "1"
  201. }
  202. array(1) {
  203. [0]=>
  204. NULL
  205. }
  206. bool(false)
  207. -- iteration 13 --
  208. array(0) {
  209. }
  210. NULL
  211. array(0) {
  212. }
  213. NULL
  214. bool(false)
  215. *** Done ***