9.207 STM_FILE_WRITE
Note: Built-In Function Rules Usage Options
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
|
Return Values
|
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