0

Hi,
I am trying to read a file but I have a problem because it is read in the end an empty line (this line does not exist)

My code is

$file_handle = fopen($newFilewithFolder, "r")
		 or exit("problem.");
$getfileInfo = fgetcsv($file_handle);
$numcols = count($getfileInfo); 

// my file is opened again and I have read a line so
// I use fseek to set in the beginning of file
fseek($file_handle, 0);
								 
$lineCounter=0;
	
while (!feof ($file_handle)) 
{
        $buffer = fgetcsv($file_handle, 4096); 
	print_r($buffer);
						
	echo "<br>";
	for ($i = 0; $i < 24; ++$i)
	{
		if ($buffer[$i] == "")
		      $buffer[$i] = " ";
	}
		
	$sex=$buffer[0];
	$age=$buffer[1];
	$pain=$buffer[2];
	//....
        $diseaseFile=$buffer[23];

        exec('java -jar program.jar '.$sex.' '.$age.' '.$pain.' ',......, $output);
			
	$rule_disease = explode(": ", $output[1]);
        $ruleFired = $rule_disease[0];
        $disease = $rule_disease[1];
	
         $lineCounter++;
			
		   
	  echo "<b>Line ".$lineCounter." : File(".$diseaseFile.")\t Jess(".$disease.")</b><br>";

what am I doing wrong ?
Thanks in advance

3
Contributors
3
Replies
6
Views
5 Years
Discussion Span
Last Post by fobos
1

what am I doing wrong ?

You're using feof() as the loop condition. feof() won't return true until after a request for input from the file fails, but by then it's too late and you'll continue processing the result of failed input. Given that fgetcsv() returns false on end-of-file, you can use it directly as the loop condition and forget about feof() entirely:

while ($buffer = fgetcsv($file_handle, 4096))
{
    echo "<br>";

    ...
}
0

thank you very very much.
It took me so much time to understand what is wrong.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.