...

Strange result with feof and break statement

nst
02-13-2006, 04:55 PM
<?php

$file_name = "test2.txt";

if(!($fp = fopen("./$file_name", "r")))
die ("File does not exist.");

for ($k=0; $k<30; $k++) {

$i++;

while($one_char!="\n") {
$one_char = fgetc($fp);
$str = $str.$one_char;
}

echo $i.": ".$str."<BR>";
$str = "";
$one_char = "";

//if(!feof($fp)) break;
}

fclose($fp);

?>


test2.txt is
A1 A2 A3
B1 B2 B3
C1 C2 C3
A1 A2 A3
B1 B2 B3
C1 C2 C3
A1 A2 A3
B1 B2 B3
C1 C2 C3
A1 A2 A3
B1 B2 B3
C1 C2 C3
A1 A2 A3
B1 B2 B3
C1 C2 C3
A1 A2 A3
B1 B2 B3
C1 C2 C3
A1 A2 A3
B1 B2 B3
C1 C2 C3
A1 A2 A3
B1 B2 B3
C1 C2 C3
A1 A2 A3
B1 B2 B3
C1 C2 C3
A1 A2 A3
B1 B2 B3
C1 C2 C3

So as it stands I get
1: A1 A2 A3
2: B1 B2 B3
3: C1 C2 C3
4: A1 A2 A3
5: B1 B2 B3
6: C1 C2 C3
7: A1 A2 A3
8: B1 B2 B3
9: C1 C2 C3
10: A1 A2 A3
11: B1 B2 B3
12: C1 C2 C3
13: A1 A2 A3
14: B1 B2 B3
15: C1 C2 C3
16: A1 A2 A3
17: B1 B2 B3
18: C1 C2 C3
19: A1 A2 A3
20: B1 B2 B3
21: C1 C2 C3
22: A1 A2 A3
23: B1 B2 B3
24: C1 C2 C3
25: A1 A2 A3
26: B1 B2 B3
27: C1 C2 C3
28: A1 A2 A3
29: B1 B2 B3
30: C1 C2 C3


but, if I uncomment the following line
if(!feof($fp)) break;
I only get
1: A1 A2 A3

I suppose that it breaks after the 1st loop, when $i is 1. Why does it do that?

I run it on WinXP, Apache2 and Php 4.3.11

Any idea?

Thanks!

chump2877
02-13-2006, 05:22 PM
You;re testing for the end of file after the first loop... obviously you're not at the end of the file yet, so your code breaks the for loop and you only get the first line of the file....

nst
02-13-2006, 06:11 PM
You 're right, I found it meanwhile...

It should be
if(feof($fp))
without the !

I guess not enough slept last night. :rolleyes:

Thanks

chump2877
02-13-2006, 06:55 PM
haha :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum