9.207 STM_FILE_WRITE
This Built-In function writes data to the specified stream file. The stream file was previously opened by a STM_FILE_OPEN.
If the stream file was opened with a LineTerminator option, then data provided in the Data Blocks will be written and terminated with the provided terminator.
If the stream file was opened with a LineTerminator=NONE option, then data provided in the Data Blocks will be written to the data stream. A line terminator may be written at the appropriate position in the data stream by use of the STM_FILE_WRITE_CTL Built-In Function.
Related Built-In Functions: STM_FILE_OPEN, STM_FILE_READ, STM_FILE_CLOSE, STM_FILE_WRITE_CTL. Refer to 9.205 STM_FILE_OPEN for the Line Terminators used.
Arguments
No | Type | Req/ Opt | Description | Min Len | Max Len | Min Dec | Max Dec |
|---|---|---|---|---|---|---|---|
1 | N | Req | File number (file handle) of the file to which data is to be written. This number was allocated to a stream file and returned on the STM_FILE_OPEN. | 1 | 3 | 0 | 0 |
2 | A | Req | Data Block 1 | 1 | Unlimited | ||
3 | A | Opt | Data Block 2 | 1 | Unlimited | ||
4 | A | Opt | Data Block 3 | 1 | Unlimited | ||
5 | A | Opt | Data Block 4 | 1 | Unlimited | ||
6 | A | Opt | Data Block 5 | 1 | Unlimited | ||
7 | A | Opt | Data Block 6 | 1 | Unlimited | ||
8 | A | Opt | Data Block 7 | 1 | Unlimited | ||
9 | A | Opt | Data Block 8 | 1 | Unlimited | ||
10 | A | Opt | Data Block 9 | 1 | Unlimited | ||
11 | A | Opt | Data Block 10 | 1 | Unlimited |
Return Values
No | Type | Req/ Opt | Description | Min Len | Max Len | Min Dec | Max Dec |
|---|---|---|---|---|---|---|---|
1 | A | Opt | Return Code OK = Data has been written ER = Error occurred. | 2 | 2 |
Example
In this IBM i example, a stream file is written to the IBM i IFS.
The file is written to directory /tmp and named example1.txt
The file is written as text and each line is terminated with a Carriage Return Line Feed. The fields in each line are not trimmed of trailing blanks, consequently the data will appear as fixed format.
FUNCTION OPTIONS(*DIRECT)
**********
DEFINE FIELD(#FILENO) TYPE(*DEC) LENGTH(3) DECIMALS(0) DESC('Allocated file number')
DEFINE FIELD(#RETNCODE) TYPE(*CHAR) LENGTH(2)
DEFINE FIELD(#OPTIONS) TYPE(*CHAR) LENGTH(256) DESC('Options for stream file open')
CHANGE FIELD(#OPTIONS) TO('''WRITE notrim Text lineTerminator= CRLF CodePage=819''')
**********
USE BUILTIN(STM_FILE_OPEN) WITH_ARGS('''/tmp/example1.txt''' #OPTIONS) TO_GET(#FILENO #RETNCODE)
IF COND('#retncode *NE OK')
MESSAGE MSGTXT('Error occurred on OPEN')
RETURN
ENDIF
**********
********** Read file PSLMST and write to the IFS file example1.txt
********** Each line written contains employee name and phone number.
********** Each line written contains employee name and phone number.
**********
SELECT FIELDS((#SURNAME) (#GIVENAME) (#PHONEBUS)) FROM_FILE(PSLMST)
USE BUILTIN(STM_FILE_WRITE) WITH_ARGS(#FILENO #SURNAME #GIVENAME #PHONEBUS) TO_GET(#RETNCODE)
IF COND('#retncode *NE OK')
MESSAGE MSGTXT('Error writing to stream file')
RETURN
ENDIF
ENDSELECT
**********
********** Close stream file and finish
**********
USE BUILTIN(STM_FILE_CLOSE) WITH_ARGS(#FILENO)
RETURN