Page 1 of 1

fgets and feof behaving unexpectedly

Posted: Mon Oct 30, 2006 6:21 am
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.......

Posted: Mon Oct 30, 2006 6:29 am
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().