fscanf_variation4.phpt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. --TEST--
  2. Test fscanf() function: usage variations - integer 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 integer format types */
  10. $file_path = dirname(__FILE__);
  11. echo "*** Test fscanf(): different integer format types with resource ***\n";
  12. // create a file
  13. $filename = "$file_path/fscanf_variation4.tmp";
  14. $file_handle = fopen($filename, "w");
  15. if($file_handle == false)
  16. exit("Error:failed to open file $filename");
  17. // non-integer type of data
  18. // resource type variable
  19. $fp = fopen (__FILE__, "r");
  20. $dfp = opendir ( dirname(__FILE__) );
  21. // array of resource types
  22. $resource_types = array (
  23. $fp,
  24. $dfp
  25. );
  26. $int_formats = array( "%d", "%hd", "%ld", "%Ld", " %d", "%d ", "% d", "\t%d", "\n%d", "%4d", "%30d", "%[0-9]", "%*d");
  27. $counter = 1;
  28. // writing to the file
  29. foreach($resource_types as $value) {
  30. @fprintf($file_handle, $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 integer formats
  42. foreach($int_formats as $int_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. var_dump( fscanf($file_handle,$int_format) );
  48. }
  49. $counter++;
  50. }
  51. // closing the resources
  52. fclose($fp);
  53. closedir($dfp);
  54. echo "\n*** Done ***";
  55. ?>
  56. --CLEAN--
  57. <?php
  58. $file_path = dirname(__FILE__);
  59. $filename = "$file_path/fscanf_variation4.tmp";
  60. unlink($filename);
  61. ?>
  62. --EXPECTF--
  63. *** Test fscanf(): different integer format types with resource ***
  64. -- iteration 1 --
  65. array(1) {
  66. [0]=>
  67. NULL
  68. }
  69. array(1) {
  70. [0]=>
  71. NULL
  72. }
  73. bool(false)
  74. -- iteration 2 --
  75. array(1) {
  76. [0]=>
  77. NULL
  78. }
  79. array(1) {
  80. [0]=>
  81. NULL
  82. }
  83. bool(false)
  84. -- iteration 3 --
  85. array(1) {
  86. [0]=>
  87. NULL
  88. }
  89. array(1) {
  90. [0]=>
  91. NULL
  92. }
  93. bool(false)
  94. -- iteration 4 --
  95. array(1) {
  96. [0]=>
  97. NULL
  98. }
  99. array(1) {
  100. [0]=>
  101. NULL
  102. }
  103. bool(false)
  104. -- iteration 5 --
  105. array(1) {
  106. [0]=>
  107. NULL
  108. }
  109. array(1) {
  110. [0]=>
  111. NULL
  112. }
  113. bool(false)
  114. -- iteration 6 --
  115. array(1) {
  116. [0]=>
  117. NULL
  118. }
  119. array(1) {
  120. [0]=>
  121. NULL
  122. }
  123. bool(false)
  124. -- iteration 7 --
  125. Warning: fscanf(): Bad scan conversion character " " in %s on line %d
  126. NULL
  127. Warning: fscanf(): Bad scan conversion character " " in %s on line %d
  128. NULL
  129. bool(false)
  130. -- iteration 8 --
  131. array(1) {
  132. [0]=>
  133. NULL
  134. }
  135. array(1) {
  136. [0]=>
  137. NULL
  138. }
  139. bool(false)
  140. -- iteration 9 --
  141. array(1) {
  142. [0]=>
  143. NULL
  144. }
  145. array(1) {
  146. [0]=>
  147. NULL
  148. }
  149. bool(false)
  150. -- iteration 10 --
  151. array(1) {
  152. [0]=>
  153. NULL
  154. }
  155. array(1) {
  156. [0]=>
  157. NULL
  158. }
  159. bool(false)
  160. -- iteration 11 --
  161. array(1) {
  162. [0]=>
  163. NULL
  164. }
  165. array(1) {
  166. [0]=>
  167. NULL
  168. }
  169. bool(false)
  170. -- iteration 12 --
  171. array(1) {
  172. [0]=>
  173. NULL
  174. }
  175. array(1) {
  176. [0]=>
  177. NULL
  178. }
  179. bool(false)
  180. -- iteration 13 --
  181. array(0) {
  182. }
  183. array(0) {
  184. }
  185. bool(false)
  186. *** Done ***