Before a channel object can be used, a channel associated with it must be opened. It can be either a file, a socket or a pipe. If a channel is not opened, any other methods associated with it will have no effect and will return a runtime error.
openFile() is used to open a file for writing, reading, or performing both these operations. The method requires 2 arguments.
Syntax:
chan_obj.openFile(filename STRING, flag STRING)
When you specify a file name, be careful with its case.
UNIX is case-sensitive, so it will treat files which names come in different cases as separate files.
|
Mode |
Flag |
Effect |
|
Read mode |
r |
Read from the file. If Path to the file is not specified, the input is performed from the file where the executable file is located. |
|
Write mode |
w |
Write to an empty file. If Path to the file is not specified, the output is performed to the directory where the executable file is located. If the file does not exist, it will be created. |
|
Append mode |
a |
Write to the end of the file (append). If Path to the file is not specified, the output is performed to the directory where the executable file is located. If the file does not exist, it will be created. |
|
Read and write mode |
u |
Read and write from and to the standard output and input. |
|
Binary mode |
b |
This flag can follow any of the listed above (e.g., "wb"). Used to avoid carriage return/line feed (CR/LF) translation. |
Usage example:
CALL chan_obj.openFile("mytext.txt", "r")
This command will open or create the file named mytext.txt in the folder on the application server where the program executable is located.
On
, the
line terminators for DOS formatted text are CR/LF. The Channel class can
be used to work with documents of this type.
On
and
,
the CR/LF line terminators are removed when a DOS file is read with the
Channel class.
When writing on
,
the CR/LF are used as line terminators. On
, only LF is used.
To avoid the translation
of CR/LF on
, add the b flag to the openFile() and openPipe()
methods. The b flag is typically used together with r or
w flags:
CALL chan_obj.OpenFile("myfile.txt", "wb")
On
, the b
flag removes only LF from CR/LF terminator when reading from a file. CR
will be added to the last field as a character value. Contrary, when the
lines are written, the b flag removes CR and leaves LF.
On
, it does not matter
whether the binary mode is used or not.