hi guys,

Need your hlep. I'm using a simple code to read a text file (line by line) using fgets() and feof(). There are only 5 lines in the text file however, the feof reports that there are 6 lines (which is not true). Can anyone help me in this? The following is how the code looks like this :
<?php
// Associate the file to the filename variable
$filename = 'bkup03.log';

// Open the file and read line by line
$fp = fopen("$filename", "r") or die("Couldn't open $filename");
$x = 1;
while (!feof($fp)) {
echo "Line No. : $x\n";
$x = $x + 1;
$line = fgets($fp);
}
fclose($fp);
?>

I've put in a counter to "debug" and noticed that it even runs beyond the 5th line and the counter keeps on increasing. Something wrong with the loop.

    Hi there.

    At a glance, your code looked fine. Upon further scrutinyt, it performed flawlessly. In a text file with 14 lines, your script reported... 14 lines.

    Check for extra space at the end of your file. Make sure you have the 'correct' line count, because it seems to me that your script is just fine.

    EDIT: Another way of finding the # of lines in a file would be this simple code:

    $file = file('myFile.txt');
    $num_lines = count($file);

      hi bradgrafelman,
      Thanks for the prompt reply. The text file that i'm using is a result of a grep result. I did this earlier :

      shell> grep <servername> bkup03.log >> bkup04.log

      Attached is the file. Any idea what's wrong with it?

      Based on your count script, it looks correct. But when i run it against my bkup04.log, it shows more than 1 line. Do rename the file to bkup04.log as the "attachment" function doesn't accept "log" extensions.

        The file you uploaded contained exactly 7 lines. I ran it through your code and it told me the same thing; 7 lines.

        Note the blank line at the end of the file; presumably, the server inserts a new line before it writes again, instead of waiting to insert it when it has something to write.

        Could this be your problem? You could always [man]rtrim/man your data to remove any extra line breaks at the very end.

          Hmm, although the log file has 6 entries (lines), the code reports that there are 7 lines. When i open it with the unix editor, i noticed that at the last line, it is empty but my cursor can still go to the 7th line.....it should not be this way. My cursor should only be able to got to the end of the 6th line.

          Do you know the command the remove any additional line? rtrim doesn't seem to work.

          regards

            Try something like...

            $file = file_get_contents('bkup04.log') or die('File error.');
            $file = rtrim($file);
            $file = explode("\n", $file);
            $num_lines = count($file);

            You will now have the number of lines in the $num_lines variable, stripping blank lines from the end of the file. (6, in this case)

              hi bradgrafelman,
              Thanks for the script. Well, i might as well show you the whole thing what i meant when i say my script isn't working. It seems that they keep processing the last line. Run this script and u'll see what i mean. It is only supposed to give 6 results....but instead give 7 results....which resulted in a division error.

              Appreciate your patience in this man... ;-)

                Alrighty. Yeah, I see it now; it's loading in that not-so-blank line and trying to explode/divide with the data that... doesn't exist! :p

                Try this script instead:

                <?php
                
                // Associate the file to the filename variable 
                $filename = 'bkup04.log';
                
                // Open the file and read line by line
                $fp = fopen("$filename", "r") or die("Couldn't open $filename");
                while (!feof($fp)) {
                	$line = fgets($fp);
                	if(strlen($line) < 2) continue;
                	$line_array = explode(" ", $line);
                	print count($line_array);
                	$line_array[7] = date("Y-m-d H:i:s", $line_array[7]);
                	$line_array[8] = $line_array[4] / $line_array[1];
                	settype($line_array[8], "integer");
                	echo "$line_array[7]\n";
                	echo "$line_array[8]\n";
                }
                fclose($fp);
                ?>

                If you'll notice my addition with the IF statement, it'll ignore any line that has less than 2 characters. Feel free to up that limit, but for now, it seems to be working.

                EDIT: Since you're hard-coding the 7th and 8th positions, you could even use count() to check if there are 6 array elements after exploding, and 'continue;' if there aren't.

                EDIT2: Since you haven't responded for a while, I'm assuming you either fell asleep at your keyboard or put this off till morning. I've experienced a succession of catastrophic events these past few days, so I think I'll turn in for the night 😉

                  hi bradgrafelman,

                  Great! It works. Hehe. Nope, not sleepin. Just freaking out in another room.....gotta be patient when it comes to programming. Anyway, ur solution works! Really appreciate it. Eventually, these data will be port over to a mysql db and i'll need to produce some graphs out of this statistics. Do you know what kinda tools/components i should use to port my stats to for ma graph? JPGraph?

                    I've never worked with any graphing tools/scripts. Sorry!

                    Glad I could help you with your other problem, however.

                      Write a Reply...