9.208 STM_FILE_WRITE_CTL

Note: Built-In Function Rules     Usage Options

This Built-In function writes Line terminator character/s to the data stream.

Related Built-In Functions: STM_FILE_OPEN, STM_FILE_CLOSE, STM_FILE_WRITE, STM_FILE_READ

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 the line terminator is to be written.

This number was allocated to a stream file by the STM_FILE_OPEN.

1

3

0

0

2

A

Opt

Line terminator control character/s to be written.

Valid values:
CR
LF  (default value)
CRLF
NL
LFCR

1

10

 

 

 

Return Values

No

Type

Req/ Opt

Description

Min Len

Max Len

Min Dec

Max Dec

2

A

Opt

Return Code

OK = Terminator 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 example2.txt

The data written contains the employee number from database file PSLMST followed by the skills code for the employee. The skills are separated by a comma. The end of the data for an employee is marked by a Carriage Return Line Feed as in this example:
A0001,ADVPGM,CS
A0008
A0013
A0090,CL,COBOL,ECD,INTRO,MANAGE1,MARKET2,MARKET3,MBA,RPG

A line terminator is not automatically written by the STM_FILE_WRITE because of the open option 'LineTerminator=NONE'.  The line terminator is written by STM_FILE_WRITE_CTL at the end of the information for an employee.

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(#COMMA) TYPE(*CHAR) LENGTH(1) DEFAULT(',')      

DEFINE     FIELD(#OPTIONS) TYPE(*CHAR) LENGTH(256) DESC('Options for stream file open')

CHANGE     FIELD(#OPTIONS) TO('''WRITE Text CodePage=819 LineTerminator=NONE''')

**********                                                       

USE        BUILTIN(STM_FILE_OPEN) WITH_ARGS('''/tmp/example2.txt''' #OPTIONS) TO_GET(#FILENO #RETNCODE)

IF         COND('#retncode *NE OK')                               

MESSAGE    MSGTXT('Error occurred on OPEN')                       

RETURN                                                            

ENDIF                                                             

**********                                                        

********** Read file PSLMST and for each employee get his skills  

********** Write to stream file employee no and list of skills    

**********  comma delimited. Terminate with a CRLF.               

**********                                                        

SELECT     FIELDS((#EMPNO)) FROM_FILE(PSLMST)                     

USE        BUILTIN(STM_FILE_WRITE) WITH_ARGS(#FILENO #EMPNO) TO_GET(#RETNCODE)

**********                                                      

SELECT     FIELDS((#SKILCODE)) FROM_FILE(PSLSKL) WITH_KEY(#EMPNO)

USE        BUILTIN(STM_FILE_WRITE) WITH_ARGS(#FILENO #COMMA #SKILCODE) TO_GET(#RETNCODE)

ENDSELECT                                                       

********** Add line terminator at end of details for employee   

USE        BUILTIN(STM_FILE_WRITE_CTL) WITH_ARGS(#FILENO CRLF) TO_GET(#RETNCODE)

ENDSELECT                                                       

**********                                                      

********** Close stream file and finish                         

**********                                                      

USE        BUILTIN(STM_FILE_CLOSE) WITH_ARGS(#FILENO)           

RETURN