Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    nst
    nst is offline
    New Coder
    Join Date
    Jul 2005
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Strange result with feof and break statement

    Code:
    <?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
    Code:
    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!

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,798
    Thanks
    19
    Thanked 156 Times in 147 Posts
    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....
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    nst
    nst is offline
    New Coder
    Join Date
    Jul 2005
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You 're right, I found it meanwhile...

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

    I guess not enough slept last night.

    Thanks
    Last edited by nst; 02-13-2006 at 06:39 PM.

  • #4
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,798
    Thanks
    19
    Thanked 156 Times in 147 Posts
    haha
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •