summaryrefslogtreecommitdiff
path: root/ext/standard/tests/file/feof_basic.phpt
blob: 78774e58e9dbbcf496d98110cbc8affaab59e0be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
--TEST--
Test feof() function : basic functionality 
--CREDITS--
Dave Kelsey <d_kelsey@uk.ibm.com>
--FILE--
<?php
/* Prototype  : proto bool feof(resource fp)
 * Description: Test for end-of-file on a file pointer 
 * Source code: ext/standard/file.c
 * Alias to functions: gzeof
 */

echo "*** Testing feof() : basic functionality ***\n";
$tmpFile1 = __FILE__.".tmp1";
$h = fopen($tmpFile1, 'wb');
$count = 10;
for ($i = 1; $i <= $count; $i++) {
   fwrite($h, "some data $i\n");
}
fclose($h);

echo "\n*** testing reading complete file using feof to stop ***\n";
$h = fopen($tmpFile1, 'rb');

//feof is not set to true until you try to read past the end of file.
//so fgets will be called even if we are at the end of the file on
//last time to set the eof flag but it will fail to read.
$lastline = "";
while (!feof($h)) {
   $previousLine = $lastline;
   $lastline = fgets($h);
}
echo $previousLine;
var_dump($lastline); // this should be false 
fclose($h);

$tmpFile2 = __FILE__.".tmp2";
$h = fopen($tmpFile2, 'wb+');
$count = 10;
echo "*** writing $count lines, testing feof ***\n";
for ($i = 1; $i <=$count; $i++) {
   fwrite($h, "some data $i\n");
   var_dump(feof($h));
}

echo "*** testing feof on unclosed file after a read ***\n";

fread($h, 100);
var_dump(feof($h));

$eofPointer = ftell($h);

echo "*** testing feof after a seek to near the beginning ***\n";
fseek($h, 20, SEEK_SET);
var_dump(feof($h));

echo "*** testing feof after a seek to end ***\n";
fseek($h, $eofPointer, SEEK_SET);
var_dump(feof($h));

echo "*** testing feof after a seek passed the end ***\n";
fseek($h, $eofPointer + 1000, SEEK_SET);
var_dump(feof($h));

echo "*** closing file, testing eof ***\n";
fclose($h);
feof($h);
unlink($tmpFile1);
unlink($tmpFile2);

echo "Done";
?>
--EXPECTF--
*** Testing feof() : basic functionality ***

*** testing reading complete file using feof to stop ***
some data 10
bool(false)
*** writing 10 lines, testing feof ***
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
*** testing feof on unclosed file after a read ***
bool(true)
*** testing feof after a seek to near the beginning ***
bool(false)
*** testing feof after a seek to end ***
bool(false)
*** testing feof after a seek passed the end ***
bool(false)
*** closing file, testing eof ***

Warning: feof(): supplied resource is not a valid stream resource in %s on line %d
Done