fscanf_variation4.phpt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. --TEST--
  2. Test fscanf() function: usage variations - integer formats with resource
  3. --FILE--
  4. <?php
  5. /* Test fscanf() to scan resource type using different integer format types */
  6. $file_path = __DIR__;
  7. echo "*** Test fscanf(): different integer format types with resource ***\n";
  8. // create a file
  9. $filename = "$file_path/fscanf_variation4.tmp";
  10. $file_handle = fopen($filename, "w");
  11. if($file_handle == false)
  12. exit("Error:failed to open file $filename");
  13. // non-integer type of data
  14. // resource type variable
  15. $fp = fopen (__FILE__, "r");
  16. $dfp = opendir ( __DIR__ );
  17. // array of resource types
  18. $resource_types = array (
  19. $fp,
  20. $dfp
  21. );
  22. $int_formats = array( "%d", "%hd", "%ld", "%Ld", " %d", "%d ", "% d", "\t%d", "\n%d", "%4d", "%30d", "%[0-9]", "%*d");
  23. $counter = 1;
  24. // writing to the file
  25. foreach($resource_types as $value) {
  26. @fprintf($file_handle, "%s", $value);
  27. @fprintf($file_handle, "\n");
  28. }
  29. // closing the file
  30. fclose($file_handle);
  31. // opening the file for reading
  32. $file_handle = fopen($filename, "r");
  33. if($file_handle == false) {
  34. exit("Error:failed to open file $filename");
  35. }
  36. $counter = 1;
  37. // reading the values from file using different integer formats
  38. foreach($int_formats as $int_format) {
  39. // rewind the file so that for every foreach iteration the file pointer starts from bof
  40. rewind($file_handle);
  41. echo "\n-- iteration $counter --\n";
  42. while( !feof($file_handle) ) {
  43. try {
  44. var_dump(fscanf($file_handle,$int_format));
  45. } catch (ValueError $exception) {
  46. echo $exception->getMessage() . "\n";
  47. }
  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 = __DIR__;
  59. $filename = "$file_path/fscanf_variation4.tmp";
  60. unlink($filename);
  61. ?>
  62. --EXPECT--
  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. Bad scan conversion character " "
  126. Bad scan conversion character " "
  127. bool(false)
  128. -- iteration 8 --
  129. array(1) {
  130. [0]=>
  131. NULL
  132. }
  133. array(1) {
  134. [0]=>
  135. NULL
  136. }
  137. bool(false)
  138. -- iteration 9 --
  139. array(1) {
  140. [0]=>
  141. NULL
  142. }
  143. array(1) {
  144. [0]=>
  145. NULL
  146. }
  147. bool(false)
  148. -- iteration 10 --
  149. array(1) {
  150. [0]=>
  151. NULL
  152. }
  153. array(1) {
  154. [0]=>
  155. NULL
  156. }
  157. bool(false)
  158. -- iteration 11 --
  159. array(1) {
  160. [0]=>
  161. NULL
  162. }
  163. array(1) {
  164. [0]=>
  165. NULL
  166. }
  167. bool(false)
  168. -- iteration 12 --
  169. array(1) {
  170. [0]=>
  171. NULL
  172. }
  173. array(1) {
  174. [0]=>
  175. NULL
  176. }
  177. bool(false)
  178. -- iteration 13 --
  179. array(0) {
  180. }
  181. array(0) {
  182. }
  183. bool(false)
  184. *** Done ***