fscanf_variation10.phpt 3.0 KB

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