fscanf_variation53.phpt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. --TEST--
  2. Test fscanf() function: usage variations - file pointer pointing to EOF
  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 read a file when file pointer is pointing to EOF */
  10. $file_path = dirname(__FILE__);
  11. echo "*** Test fscanf(): to read a file when file pointer is pointing to EOF ***\n";
  12. // various formats
  13. $formats = array( "%d", "%f", "%e", "%u", " %s", "%x", "%o");
  14. $counter = 1;
  15. // various read modes
  16. $modes = array("r", "rb", "rt", "r+", "r+b", "r+t",
  17. "w+", "w+b", "w+t",
  18. "a+", "a+b", "a+t"
  19. );
  20. $counter = 1;
  21. // reading the values from file using different integer formats
  22. foreach($modes as $mode) {
  23. // create an empty file
  24. $filename = "$file_path/fscanf_variation53.tmp";
  25. $file_handle = fopen($filename, "w");
  26. if($file_handle == false)
  27. exit("Error:failed to open file $filename");
  28. //writing data to the file
  29. @fwrite($file_handle, "Sample text\n");
  30. // writing a blank line
  31. @fwrite($file_handle, "\n");
  32. //closing the file
  33. fclose($file_handle);
  34. // opening file in $mode mode
  35. $file_handle = fopen($filename, $mode);
  36. if($file_handle == false) {
  37. exit("Error:failed to open file $filename");
  38. }
  39. echo "\n-- iteration $counter --\n";
  40. // current location
  41. var_dump( ftell($file_handle) );
  42. // set the file pointer to eof
  43. var_dump( fseek($file_handle, 0, SEEK_END) );
  44. // current location
  45. var_dump( ftell($file_handle) );
  46. foreach($formats as $format) {
  47. var_dump( fscanf($file_handle,$format) );
  48. }
  49. $counter++;
  50. fclose($file_handle);
  51. unlink($filename);
  52. }
  53. echo "\n*** Done ***";
  54. ?>
  55. --CLEAN--
  56. <?php
  57. $file_path = dirname(__FILE__);
  58. $filename = "$file_path/fscanf_variation53.tmp";
  59. if(file_exists($filename)) {
  60. unlink($filename);
  61. }
  62. ?>
  63. --EXPECT--
  64. *** Test fscanf(): to read a file when file pointer is pointing to EOF ***
  65. -- iteration 1 --
  66. int(0)
  67. int(0)
  68. int(13)
  69. bool(false)
  70. bool(false)
  71. bool(false)
  72. bool(false)
  73. bool(false)
  74. bool(false)
  75. bool(false)
  76. -- iteration 2 --
  77. int(0)
  78. int(0)
  79. int(13)
  80. bool(false)
  81. bool(false)
  82. bool(false)
  83. bool(false)
  84. bool(false)
  85. bool(false)
  86. bool(false)
  87. -- iteration 3 --
  88. int(0)
  89. int(0)
  90. int(13)
  91. bool(false)
  92. bool(false)
  93. bool(false)
  94. bool(false)
  95. bool(false)
  96. bool(false)
  97. bool(false)
  98. -- iteration 4 --
  99. int(0)
  100. int(0)
  101. int(13)
  102. bool(false)
  103. bool(false)
  104. bool(false)
  105. bool(false)
  106. bool(false)
  107. bool(false)
  108. bool(false)
  109. -- iteration 5 --
  110. int(0)
  111. int(0)
  112. int(13)
  113. bool(false)
  114. bool(false)
  115. bool(false)
  116. bool(false)
  117. bool(false)
  118. bool(false)
  119. bool(false)
  120. -- iteration 6 --
  121. int(0)
  122. int(0)
  123. int(13)
  124. bool(false)
  125. bool(false)
  126. bool(false)
  127. bool(false)
  128. bool(false)
  129. bool(false)
  130. bool(false)
  131. -- iteration 7 --
  132. int(0)
  133. int(0)
  134. int(0)
  135. bool(false)
  136. bool(false)
  137. bool(false)
  138. bool(false)
  139. bool(false)
  140. bool(false)
  141. bool(false)
  142. -- iteration 8 --
  143. int(0)
  144. int(0)
  145. int(0)
  146. bool(false)
  147. bool(false)
  148. bool(false)
  149. bool(false)
  150. bool(false)
  151. bool(false)
  152. bool(false)
  153. -- iteration 9 --
  154. int(0)
  155. int(0)
  156. int(0)
  157. bool(false)
  158. bool(false)
  159. bool(false)
  160. bool(false)
  161. bool(false)
  162. bool(false)
  163. bool(false)
  164. -- iteration 10 --
  165. int(0)
  166. int(0)
  167. int(13)
  168. bool(false)
  169. bool(false)
  170. bool(false)
  171. bool(false)
  172. bool(false)
  173. bool(false)
  174. bool(false)
  175. -- iteration 11 --
  176. int(0)
  177. int(0)
  178. int(13)
  179. bool(false)
  180. bool(false)
  181. bool(false)
  182. bool(false)
  183. bool(false)
  184. bool(false)
  185. bool(false)
  186. -- iteration 12 --
  187. int(0)
  188. int(0)
  189. int(13)
  190. bool(false)
  191. bool(false)
  192. bool(false)
  193. bool(false)
  194. bool(false)
  195. bool(false)
  196. bool(false)
  197. *** Done ***