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

Recommended Answers

All 3 Replies

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>";

    ...
}

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

MARK SOLVED if this is solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.