fscanf_variation32.phpt 3.1 KB

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