feof_basic.phpt 2.4 KB

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