Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_php thread: file reading and reading an extra line


Message #1 by "Adam Lang" <aalang@r...> on Tue, 22 Jan 2002 16:20:40 -0500
Thanks Nikolai and Jacob, I used both your emails for the solution.

YEs, the problem was fgets didn't return EOF until it actually was used
again after I read my last line.

So, using the file() function, I read the file into an array and they hopped
the array searching for the values I needed.  Following is the fixed
function.


function getEventDates($months) {
// This function opens the file for each month/year and grabs the dates.

// Convert the timestamp to the appropriate month/year name.
   $filename = getYearMonth($months);
   // Open the month file and check to make sure it was successful.
   if (file_exists($filename)) {
      $filearray=file($filename);
      // Read dates into array
      $count=0;
      while (list (, $str) = each ($filearray)) {
         // extract the first 10 characters, that is the timestamp
         $temp_array = getdate(substr($str, 0,10));
         $eventDates[$count] = $temp_array["mday"];
         $count++;
      }
   } else {
     $eventDates = array();
   }

return $eventDates;

}

Adam Lang
Systems Engineer
Rutgers Casualty Insurance Company
http://www.rutgersinsurance.com
----- Original Message -----
From: "Nikolai Devereaux" <yomama@u...>
To: "professional php" <pro_php@p...>
Sent: Tuesday, January 22, 2002 4:59 PM
Subject: [pro_php] RE: file reading and reading an extra line


>
> Hmmm... does your file have a newline char at the end of the first line
> and/or a blank line after your "1015131600test" line?
>
> Since I don't have your actual file, have you tried printing the value of
> $str after your fgets call to see what's in it?
>
> I don't understand what you mean by "no value to match the second key".
> What's the second key?  Is it an integer index, or actual text from the
> file?
>
> Lastly... are your files going to be big?  I wonder why you don't read the
> file in to a string array with file() and iterate across the array
elements
> instead... seems that it would execute quicker since you're not making
> multiple accesses into the file.
>
> nik
>
> > -----Original Message-----
> > From: Adam Lang [mailto:aalang@r...]
> > Sent: Tuesday, January 22, 2002 1:21 PM
> > To: professional php
> > Subject: [pro_php] file reading and reading an extra line
> >
> >
> > I am not sure if I am just staring at this code too long or what, but I
> > can't see why I am having this problem.
> >
> > Here is my function (the line that is "echo $count . " " . $str .
> > $temp_array["mday"] . "<BR>";" is to see the values that are
> > going into the
> > array):
> >
> > function getEventDates($months) {
> > // This function opens the file for each month/year and grabs the dates.
> >
> > // Convert the timestamp to the appropriate month/year name.
> >    $filename = getYearMonth($months);
> >    // Open the month file and check to make sure it was successful.
> >    if (file_exists($filename)) {
> >       $fp=fopen ($filename, "r");
> >       // Read dates into array
> >       $count = 0;
> >       while (!feof ($fp)) {
> >          $str = fgets($fp, 4096);
> >          // extract the first 10 characters, that is the timestamp
> >          $temp_array = getdate(substr($str, 0,10));
> >          $eventDates[$count] = $temp_array["mday"];
> > echo $count . " " . $str . $temp_array["mday"] . "<BR>";
> >          $count++;
> >       }
> >       fclose($fp);
> >    } else {
> >      $eventDates = array();
> >    }
> >
> > return $eventDates;
> >
> > }
> >
> > Here is my file I am reading in (just one line):
> >
> > 1015131600test
> >
> > Why am I getting two iterations out of the while loop?  I have
> > one line, so
> > I want one entry in my array, but I am getting two, with no value
> > to matche
> > the second key.
> >
> > Adam Lang
> > Systems Engineer
> > Rutgers Casualty Insurance Company
> > http://www.rutgersinsurance.com
> >
> >
> >
> >
>
>
>




  Return to Index