fgets() help

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have a fixed length file that I am reading and trying to process. The
file contains 3 types of records (a,b,c) each has it's own field
definition for the  position on the line. Record type is always held in
position 2(php 0,1,2), with a length of 1.

I would like to process each line seperatley, evaluating the record
type, and then taking the appropriate action. Like so:

//open the import file
$import_file = fopen(dirname(__FILE__)."/../../data/imports/data.imp",

//make sure we can read it
        //loop to read each line
    while (!feof($import_file))
            //read line and process accordingly
           $line = fgets($import_file);
            $record_type = substr($line, 2,1);
                //process 'a' types
                //process 'b' types
                //process 'c' types
             }//end loop

I can read the data just fine, my problem is that all records are being
treated as 'a' records. Any help would be greatly appreciated.


Re: fgets() help

diroddi wrote:
Quoted text here. Click to load it

You omitted "$" in a couple of places:

Quoted text here. Click to load it

The above is a correctly written line.

Quoted text here. Click to load it

The two lines above, however, refer to a (probably undefined)
constant record_type, not to the variable $record_type...

An unrelated suggestion: your code can be streamlined like

if($import_file = fopen(dirname(__FILE__) .
                  "/../../data/imports/data.imp", "r")) {
  while ($line = fgets($import_file)) {
    $record_type = substr($line, 2,1);
    switch ($record_type) {
      case 'a':
        // Process 'a' record
      case 'b':
        // Process 'b' record
      case 'c':
        // Process 'c' record
} else {
  // Handle file opening error.


Site Timeline