fscanf_variation50.phpt 3.1 KB

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