base.Channel.write()

write() is used to write data records to a channel and add delimiters to separate fields. The method requires 2 arguments.

The write mode will depend on the way you opened the channel. Thus if a channel was opened for writing (w flag) and you write to a file, the contents of the file will be replaced by the written values. If you want to add more lines to a file without erasing the existing content, use a flag for appending.

The channel must be open for writing, otherwise this method will cause a runtime error. Make sure that the method for opening the channel includes a, u or w flag as a parameter.

Syntax:

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

chan_obj.Write([myrec.*])

Using line terminators with read() and write():

read() and write() methods transfer the escaped line-feed characters BS+LF. This is how they are written to the output. BS+LF are detected and transferred back when a reading operation is performed.

If you want to add a line-feed character to a value, the backslash and the line-feed should be given as two independent characters. When the value is written to the string constant, the backslash should be escaped.

In this example, we use an empty delimiter in order to make the explanation simpler:

MAIN
 DEFINE chan_obj base.Channel
 DEFINE cTxt STRING
 DEFINE i INT

    LET chan_obj = base.Channel.create()
    CALL chan_obj.setDelimiter("")
    CALL chan_obj.openFile("out.txt","w")
    CALL chan_obj.write("aaa\\\nbbb") -- [aaa<bs><lf>bbb]
    CALL chan_obj.write("ccc\nddd")   -- [aaa<lf>bbb]
    
    CALL chan_obj.openFile("out.txt","r")
    LET i=1
    WHILE chan_obj.read([cTxt])    
       DISPLAY i
       DISPLAY cTxt
       LET i=i+1
    END WHILE
END MAIN 

The code above will result in this output:

          1
aaa\
bbb
          2
ccc
          3
ddd

The first and the second lines of this file will get to the same line in a Channel record.

read() will interpret these lines into these strings:

{Read 1} aaa<bs><lf>bbb

{Read 2} ccc

{Read 3} ddd

These lines are equal to these assignments in string constants:

LET str = "aaa\\\nbbb"

LET str = "ccc"

LET str = "ddd"

 

 

Contact Us

Privacy Policy

Copyright © 2024 Querix, (UK) Ltd.