File input/output in CL400
In CL400, file input/output (I/O) is used to read and write data from and to files. There are two types of files in CL400: sequential and random access files. In this answer, we'll explain how to work with both types of files and provide code examples for each.
- Sequential Files:
A sequential file is a file that contains records that are accessed in order, from beginning to end. Here's how you can work with sequential files in CL400:
- Opening a Sequential File:
To open a sequential file, you can use the OVRDBF (Override Database File) command. Here's an example:
OVRDBF FILE(INPUTFILE) TOFILE(LIBRARY/SEQFILE) MBR(MEMBERNAME)
In this example, we have overridden the INPUTFILE with the SEQFILE member MEMBERNAME in the LIBRARY library. The SEQFILE must already exist.
- Reading Records from a Sequential File:
To read records from a sequential file, you can use the RCVF (Receive File) command. Here's an example:
DCL VAR(&RECORD) TYPE(*CHAR) LEN(100)
DCL VAR(&EOF) TYPE(*LGL)
DCL VAR(&COUNT) TYPE(*INT) VALUE(0)
DO WHILE COND(&EOF = *OFF)
RCVF FILE(INPUTFILE) RCDFMT(MYFORMAT) EOF(&EOF)
IF COND(&EOF = *OFF) THEN(DO)
CHGVAR VAR(&RECORD) VALUE(MYFIELD)
/* process record */
CHGVAR VAR(&COUNT) VALUE(&COUNT + 1)
ENDDO
ENDDO
SNDPGMMSG MSG('Records read: ' || &COUNT)
In this example, we have declared a character variable named &RECORD, a logical variable named &EOF, and an integer variable named &COUNT. We then read records from the INPUTFILE using the RCVF command and a custom record format named MYFORMAT. We process each record and count the number of records read. We loop until the end of the file is reached.
- Writing Records to a Sequential File:
To write records to a sequential file, you can use the CPYTOIMPF (Copy to Import File) command. Here's an example:
CPYTOIMPF FROMFILE(MYFILE) TOSTMF('/QSYS.LIB/LIBRARY.MBR.MEMBER.FILE') MBROPT(*REPLACE) RCDDLM(*CRLF) FLDDLM(';') STRDLM(*NONE)
In this example, we have copied the contents of the MYFILE database file to a text file named FILE in the MEMBER member of the LIBRARY library. The fields in the database file are separated by semicolons, and the records are separated by carriage return and line feed characters.
- Random Access Files:
A random access file is a file that contains records that can be accessed randomly, based on a key field. Here's how you can work with random access files in CL400:
- Opening a Random Access File:
To open a random access file, you can use the OPNQRYF (Open Query File) command. Here's an example:
OPNQRYF FILE(MYFILE) KEYFLD(KEYFIELD) SEQ(*KEYED) POSITION(*AFT)
In this example, we have opened the MYFILE database file and specified the KEYFIELD as the key field. The SEQ parameter specifies that the file is keyed, and the POSITION parameter specifies that the pointer should be positioned after the last record read.
- Reading Records from a Random Access File:
To read records from a random access file, you can use the RCVF command, just like you would with a sequential file. However, instead of specifying the file name and record format, you specify the file name and record format name as parameters to the command. Here's an example:
DCL VAR(&RECORD) TYPE(*CHAR) LEN(100)
DCL VAR(&EOF) TYPE(*LGL)
DCL VAR(&COUNT) TYPE(*INT) VALUE(0)
DO WHILE COND(&EOF = *OFF)
RCVF FILE(MYFILE) RCDFMT(MYFORMAT) EOF(&EOF)
IF COND(&EOF = *OFF) THEN(DO)
CHGVAR VAR(&RECORD) VALUE(MYFIELD)
/* process record */
CHGVAR VAR(&COUNT) VALUE(&COUNT + 1)
ENDDO
ENDDO
SNDPGMMSG MSG('Records read: ' || &COUNT)
In this example, we have declared a character variable named &RECORD, a logical variable named &EOF, and an integer variable named &COUNT. We then read records from the MYFILE database file using the RCVF command and a custom record format named MYFORMAT. We process each record and count the number of records read. We loop until the end of the file is reached.
- Writing Records to a Random Access File:
To write records to a random access file, you can use the ADDPFM (Add Physical File Member) command. Here's an example:
ADDPFM FILE(MYFILE) MBR(MYMEMBER) RCDDATA('MYFIELD1' 'MYFIELD2')
In this example, we have added a record to the MYFILE database file in the MYMEMBER member. The record contains two fields: MYFIELD1 and MYFIELD2.
These are some examples of how to work with sequential and random access files in CL400. By using these commands and techniques, you can read from and write to files in CL400 with ease.
Leave a Comment