feof_basic.phpt 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. --TEST--
  2. Test feof() function : basic functionality
  3. --CREDITS--
  4. Dave Kelsey <d_kelsey@uk.ibm.com>
  5. --FILE--
  6. <?php
  7. echo "*** Testing feof() : basic functionality ***\n";
  8. $tmpFile1 = __FILE__.".tmp1";
  9. $h = fopen($tmpFile1, 'wb');
  10. $count = 10;
  11. for ($i = 1; $i <= $count; $i++) {
  12. fwrite($h, "some data $i\n");
  13. }
  14. fclose($h);
  15. echo "\n*** testing reading complete file using feof to stop ***\n";
  16. $h = fopen($tmpFile1, 'rb');
  17. //feof is not set to true until you try to read past the end of file.
  18. //so fgets will be called even if we are at the end of the file on
  19. //last time to set the eof flag but it will fail to read.
  20. $lastline = "";
  21. while (!feof($h)) {
  22. $previousLine = $lastline;
  23. $lastline = fgets($h);
  24. }
  25. echo $previousLine;
  26. var_dump($lastline); // this should be false
  27. fclose($h);
  28. $tmpFile2 = __FILE__.".tmp2";
  29. $h = fopen($tmpFile2, 'wb+');
  30. $count = 10;
  31. echo "*** writing $count lines, testing feof ***\n";
  32. for ($i = 1; $i <=$count; $i++) {
  33. fwrite($h, "some data $i\n");
  34. var_dump(feof($h));
  35. }
  36. echo "*** testing feof on unclosed file after a read ***\n";
  37. fread($h, 100);
  38. var_dump(feof($h));
  39. $eofPointer = ftell($h);
  40. echo "*** testing feof after a seek to near the beginning ***\n";
  41. fseek($h, 20, SEEK_SET);
  42. var_dump(feof($h));
  43. echo "*** testing feof after a seek to end ***\n";
  44. fseek($h, $eofPointer, SEEK_SET);
  45. var_dump(feof($h));
  46. echo "*** testing feof after a seek passed the end ***\n";
  47. fseek($h, $eofPointer + 1000, SEEK_SET);
  48. var_dump(feof($h));
  49. echo "*** closing file, testing eof ***\n";
  50. fclose($h);
  51. try {
  52. feof($h);
  53. } catch (TypeError $e) {
  54. echo $e->getMessage(), "\n";
  55. }
  56. unlink($tmpFile1);
  57. unlink($tmpFile2);
  58. echo "Done";
  59. ?>
  60. --EXPECT--
  61. *** Testing feof() : basic functionality ***
  62. *** testing reading complete file using feof to stop ***
  63. some data 10
  64. bool(false)
  65. *** writing 10 lines, testing feof ***
  66. bool(false)
  67. bool(false)
  68. bool(false)
  69. bool(false)
  70. bool(false)
  71. bool(false)
  72. bool(false)
  73. bool(false)
  74. bool(false)
  75. bool(false)
  76. *** testing feof on unclosed file after a read ***
  77. bool(true)
  78. *** testing feof after a seek to near the beginning ***
  79. bool(false)
  80. *** testing feof after a seek to end ***
  81. bool(false)
  82. *** testing feof after a seek passed the end ***
  83. bool(false)
  84. *** closing file, testing eof ***
  85. feof(): supplied resource is not a valid stream resource
  86. Done