fgets and feof behaving unexpectedly

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

fgets and feof behaving unexpectedly

Post by malcolmboston »

i have this in a file that im parsing

Code: Select all

[ID] FS1024583248, 170128, 24/10/2006 04:02:02
[T] 07 43212991
[M] 
[E] trentxxxx@hotmail.com
[A] Mr Trent Hatton, 27 High St, , Millaa Millaa, QLD, 4886, Australia
[C] Yes
[R] http://www.redsquareclothing.com/
[I] 72156,55DSL Classic Logo Tee,£24.9899997711182,Med,1,3
[D] 15.50
[END]
now when i parse it with the following function......

Code: Select all

function readFileByLine ($filePath) {
      $fp = fopen($filePath, "r");
      $current_line = fgets($fp);
      while (!feof($fp)) {
         // process current line
         $this->currentLineContent = fgets($fp);
         $this->LineDelimiter = $this->getDelimiter ($this->currentLineContent);
         $this->sendToAction ($this->LineDelimiter, $this->currentLineContent);
      }
      fclose($fp);
   }
i get the following delimiters being parsed

Code: Select all

Delimeter = [T]
Delimeter = [M]
Delimeter = [E]
Delimeter = [A]
Delimeter = [C]
Delimeter = [I]
Delimeter = [D]
......missing out the first line, does anyone have any idea as to why its doing this, i was reading on the PHP manual that there were various problems with feof and that it was an unreliable function.......
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Your reading the first line of the file before the while loop, and first line in the while loop reads the next line which you then process. Just remove the first fgets().
Post Reply