base.Channel.read()

 

The Read() method is used to read data records in which delimiters are used to separate fields. When using this method, you must specify the list of the variables which will receive the read values. The list should be given in square braces:

 

CALL chan_obj.Read([val1,val2,val3])

 

Here, you can also use a variable of RECORD data type:

 

CALL chan_obj.Read([myrec.*])

 

If the Read() method successfully reads the data, it return TRUE. This means that the values specified in the method arguments are searched through the file and, if they are found, TRUE is returned, and the found values are not modified in any way. Otherwise, the returned value is FALSE, which means that the method has reached the end of the stream or the file and was not able to find the specified values.

For example, if the method can be used for the following file:

 

$AAAA$BBBBB$CCCCC$DDDDD$BBBBB

XXXXX$YYYYYY$ZZZZZZ$

123$fd24$$

 

In this case the code looking for the values in the last line of this file will be as follows:

 

MAIN

  DEFINE chan_obj base.Channel

  DEFINE tRec RECORD

           a,b,c,d,e,f STRING

         END RECORD

  LET chan_obj = base.Channel.Create()

  CALL chan_obj.openFile("test4.txt", "r")

  CALL chan_obj.setDelimiter("$")

  

  WHILE chan_obj.read([tRec.*])  --will return 1 (TRUE), because the line can be read.

    DISPLAY tRec.*

  END WHILE

END MAIN

 

 

The channel must be open for reading, otherwise this method will cause a runtime error. Make sure that the u or r flag is passed to the method for opening the channel as a parameter.

 

 

 

Related topics:

base.Channel.setDelimiter