fscanf_variation22.phpt 3.3 KB

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