fscanf_variation41.phpt 2.9 KB

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