fscanf_variation16.phpt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. --TEST--
  2. Test fscanf() function: usage variations - string formats with resource
  3. --FILE--
  4. <?php
  5. /* Test fscanf() to scan resource type using different string format types */
  6. $file_path = __DIR__;
  7. echo "*** Test fscanf(): different string format types with resource ***\n";
  8. // create a file
  9. $filename = "$file_path/fscanf_variation16.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. $string_formats = array( "%s",
  22. "%hs", "%ls", "%Ls",
  23. " %s", "%s ", "% s",
  24. "\t%s", "\n%s", "%4s",
  25. "%30s", "%[a-zA-Z0-9]", "%*s");
  26. $counter = 1;
  27. // writing to the file
  28. foreach($resource_types as $value) {
  29. @fprintf($file_handle, "%s", $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 string formats
  41. foreach($string_formats as $string_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,$string_format));
  48. } catch (ValueError $exception) {
  49. echo $exception->getMessage() . "\n";
  50. }
  51. }
  52. $counter++;
  53. }
  54. // closing the resources
  55. fclose($fp);
  56. closedir($dfp);
  57. echo "\n*** Done ***";
  58. ?>
  59. --CLEAN--
  60. <?php
  61. $file_path = __DIR__;
  62. $filename = "$file_path/fscanf_variation16.tmp";
  63. unlink($filename);
  64. ?>
  65. --EXPECT--
  66. *** Test fscanf(): different string format types with resource ***
  67. -- iteration 1 --
  68. array(1) {
  69. [0]=>
  70. string(8) "Resource"
  71. }
  72. array(1) {
  73. [0]=>
  74. string(8) "Resource"
  75. }
  76. bool(false)
  77. -- iteration 2 --
  78. array(1) {
  79. [0]=>
  80. string(8) "Resource"
  81. }
  82. array(1) {
  83. [0]=>
  84. string(8) "Resource"
  85. }
  86. bool(false)
  87. -- iteration 3 --
  88. array(1) {
  89. [0]=>
  90. string(8) "Resource"
  91. }
  92. array(1) {
  93. [0]=>
  94. string(8) "Resource"
  95. }
  96. bool(false)
  97. -- iteration 4 --
  98. array(1) {
  99. [0]=>
  100. string(8) "Resource"
  101. }
  102. array(1) {
  103. [0]=>
  104. string(8) "Resource"
  105. }
  106. bool(false)
  107. -- iteration 5 --
  108. array(1) {
  109. [0]=>
  110. string(8) "Resource"
  111. }
  112. array(1) {
  113. [0]=>
  114. string(8) "Resource"
  115. }
  116. bool(false)
  117. -- iteration 6 --
  118. array(1) {
  119. [0]=>
  120. string(8) "Resource"
  121. }
  122. array(1) {
  123. [0]=>
  124. string(8) "Resource"
  125. }
  126. bool(false)
  127. -- iteration 7 --
  128. Bad scan conversion character " "
  129. Bad scan conversion character " "
  130. bool(false)
  131. -- iteration 8 --
  132. array(1) {
  133. [0]=>
  134. string(8) "Resource"
  135. }
  136. array(1) {
  137. [0]=>
  138. string(8) "Resource"
  139. }
  140. bool(false)
  141. -- iteration 9 --
  142. array(1) {
  143. [0]=>
  144. string(8) "Resource"
  145. }
  146. array(1) {
  147. [0]=>
  148. string(8) "Resource"
  149. }
  150. bool(false)
  151. -- iteration 10 --
  152. array(1) {
  153. [0]=>
  154. string(4) "Reso"
  155. }
  156. array(1) {
  157. [0]=>
  158. string(4) "Reso"
  159. }
  160. bool(false)
  161. -- iteration 11 --
  162. array(1) {
  163. [0]=>
  164. string(8) "Resource"
  165. }
  166. array(1) {
  167. [0]=>
  168. string(8) "Resource"
  169. }
  170. bool(false)
  171. -- iteration 12 --
  172. array(1) {
  173. [0]=>
  174. string(8) "Resource"
  175. }
  176. array(1) {
  177. [0]=>
  178. string(8) "Resource"
  179. }
  180. bool(false)
  181. -- iteration 13 --
  182. array(0) {
  183. }
  184. array(0) {
  185. }
  186. bool(false)
  187. *** Done ***