base.Channel.read()

read() is used to read data records in which delimiters are used to separate fields. The method requires 2 arguments.

If read() successfully reads the data, it returns 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.

Syntax:

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

chan_obj.Read([myrec.*])

Usage example:

Example reads file with the next content:

$AAAA$BBBBB$CCCCC$DDDDD$BBBBB

XXXXX$YYYYYY$ZZZZZZ$

123$fd24$$

 

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.

Errors handling

It is possible to handle errors occurring during read or write operations by means of WHENEVER ERROR statement, since the errors will change the value of the STATUS built-in variable:

WHENEVER ERROR CONTINUE

  CALL chan_obj.Read([num,label])

    IF STATUS THEN

      ERROR "A reading error occurred"

      CALL chan_obj.close()

      RETURN -1

    END IF

WHENEVER ERROR STOP

 

 

Contact Us

Privacy Policy

Copyright © 2024 Querix, (UK) Ltd.