fgetc_variation1.phpt 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. --TEST--
  2. Test fgetc() function : usage variations - read when file pointer at EOF
  3. --FILE--
  4. <?php
  5. /*
  6. Prototype: string fgetc ( resource $handle );
  7. Description: Gets character from file pointer
  8. */
  9. // include the header for common test function
  10. include ("file.inc");
  11. echo "*** Testing fgetc() : usage variations ***\n";
  12. echo "-- Testing fgetc() with file whose file pointer is pointing to EOF --\n";
  13. // create a file
  14. create_files(dirname(__FILE__), 1, "text_with_new_line", 0755, 1, "w", "fgetc_variation");
  15. $filename = dirname(__FILE__)."/fgetc_variation1.tmp";
  16. // loop to check the file opened in different read modes
  17. $file_modes = array("r", "rb", "rt", "r+", "r+b", "r+t");
  18. $loop_counter =0;
  19. for(; $loop_counter < count($file_modes); $loop_counter++) {
  20. // print the hearder
  21. echo "-- File opened in mode : $file_modes[$loop_counter] --\n";
  22. // open the file
  23. $file_handle = fopen ($filename, $file_modes[$loop_counter]);
  24. if (!$file_handle) {
  25. echo "Error: failed to open file $filename! \n";
  26. exit();
  27. }
  28. // seek to end of the file and try fgetc()
  29. var_dump( fseek($file_handle, 0, SEEK_END) ); // set file pointer to eof
  30. var_dump( feof($file_handle) ); // expected false
  31. var_dump( ftell($file_handle) ); // ensure that file pointer is at eof
  32. var_dump( fgetc($file_handle) ); // try n read a char, none expected
  33. var_dump( feof($file_handle) ); // ensure thta file pointer is at eof
  34. var_dump( ftell($file_handle) ); // file pointer position
  35. // close the file handle
  36. fclose($file_handle);
  37. }
  38. echo "Done\n";
  39. ?>
  40. --CLEAN--
  41. <?php
  42. unlink( dirname(__FILE__)."/fgetc_variation1.tmp");
  43. ?>
  44. --EXPECTF--
  45. *** Testing fgetc() : usage variations ***
  46. -- Testing fgetc() with file whose file pointer is pointing to EOF --
  47. -- File opened in mode : r --
  48. int(0)
  49. bool(false)
  50. int(1024)
  51. bool(false)
  52. bool(true)
  53. int(1024)
  54. -- File opened in mode : rb --
  55. int(0)
  56. bool(false)
  57. int(1024)
  58. bool(false)
  59. bool(true)
  60. int(1024)
  61. -- File opened in mode : rt --
  62. int(0)
  63. bool(false)
  64. int(1024)
  65. bool(false)
  66. bool(true)
  67. int(1024)
  68. -- File opened in mode : r+ --
  69. int(0)
  70. bool(false)
  71. int(1024)
  72. bool(false)
  73. bool(true)
  74. int(1024)
  75. -- File opened in mode : r+b --
  76. int(0)
  77. bool(false)
  78. int(1024)
  79. bool(false)
  80. bool(true)
  81. int(1024)
  82. -- File opened in mode : r+t --
  83. int(0)
  84. bool(false)
  85. int(1024)
  86. bool(false)
  87. bool(true)
  88. int(1024)
  89. Done