Learn to write and read text files in RPGLE on IBM i with this practical lesson. This guide uses clear examples from the provided source code, originally published on Scott Klements website.
In this lesson we will create an IFS text file, edit it, and read it back.
Understand the Program Structure
The example program, CH5TEXT, performs three tasks:
- Creates a text file in the Integrated File System (IFS).
- Opens the file for editing with the IBM i text editor.
- Reads the file and displays each line’s first 52 bytes.
Step 1: Set Up Your Program
Start by defining the program’s foundation. Use these compiler directives and include necessary prototypes.
H DFTACTGRP(*NO) ACTGRP(*NEW) BNDDIR('QC2LE') BNDDIR('IFSTEXT')
D/copy IFSEBOOK/QRPGLESRC,IFSIO_H
D/copy IFSEBOOK/QRPGLESRC,ERRNO_H
D/copy IFSEBOOK/QRPGLESRC,IFSTEXT_H
H-Spec: Sets default activation group to *NO, creates a new activation group, and binds to QC2LE and IFSTEXT directories.
D-Spec Copies: Include IFSIO_H for file operations, ERRNO_H for error handling, and IFSTEXT_H for text file utilities.
Step 2: Define Variables and Prototypes
Declare variables for file handling and text processing.
D Cmd PR ExtPgm('QCMDEXC')
D command 200A const
D len 15P 5 const
D fd S 10I 0
D line S 100A
D len S 10I 0
D msg S 52A
Cmd: Prototype for QCMDEXC to run system commands.
fd: File descriptor for IFS file operations.
line: Holds a line of text, up to 100 characters.
len: Tracks the length of the line.
msg: Displays up to 52 bytes of each line.
Step 3: Create the Text File
Write text to a file in the IFS. Use the MakeFile subroutine.
C**************************************************************
C* Write some text to a text file
C**************************************************************
CSR MakeFile begsr
C*------------------------
c eval fd = open('/ifstest/ch5_file.txt':
c O_TRUNC+O_CREAT+O_WRONLY:
c S_IWUSR+S_IRUSR+S_IRGRP+S_IROTH)
c if fd < 0
c callp die('open(): ' + %str(strerror(errno)))
c endif
c eval line = 'Dear Cousin,'
c eval len = %len(%trimr(line))
c callp writeline(fd: %addr(line): len)
c eval line = ' '
c eval len = 0
c callp writeline(fd: %addr(line): len)
c eval line = 'I love the way you make' +
c ' cheese fondue.'
c eval len = %len(%trimr(line))
c callp writeline(fd: %addr(line): len)
c eval line = ' '
c eval len = 0
c callp writeline(fd: %addr(line): len)
c eval line = 'Thank you for being so cheesy!'
c eval len = %len(%trimr(line))
c callp writeline(fd: %addr(line): len)
c eval line = ' '
c eval len = 0
c callp writeline(fd: %addr(line): len)
c eval line = 'Sincerely,'
c eval len = %len(%trimr(line))
c callp writeline(fd: %addr(line): len)
c eval line = ' Richard M. Nixon'
c eval len = %len(%trimr(line))
c callp writeline(fd: %addr(line): len)
c callp close(fd)
C*------------------------
CSR endsr
Open the File: Use open() with flags O_TRUNC (clear existing file), O_CREAT (create if not exists), and O_WRONLY (write-only). Set permissions for user, group, and others.
Write Lines: Assign text to line, calculate its length with %len(%trimr(line)), and use writeline() to write. Add blank lines for spacing.
Close the File: Call close(fd) to save changes.
Step 4: Edit the File
Allow users to modify the file with the IBM i text editor.
C**************************************************************
C* Call the OS/400 text editor, and let the user change the
C* text around.
C**************************************************************
CSR EditFile begsr
C*------------------------
c callp cmd('EDTF STMF(''/ifstest/' +
c 'ch5_file.txt'')': 200)
C*------------------------
CSR endsr
Run EDTF: Use the cmd() procedure to call EDTF, passing the file path. The 200 specifies the command length.
Step 5: Read and Display the File
C**************************************************************
C* Read file, line by line, and dsply what fits
C* (DSPLY has a lousy 52-byte max... blech)
C**************************************************************
CSR ShowFile begsr
C*------------------------
c eval fd = open('/ifstest/ch5_file.txt':
c O_RDONLY)
c if fd < 0
c callp die('open(): ' + %str(strerror(errno)))
c endif
c dow readline(fd: %addr(line): %size(line))>=0
c eval Msg = line
c Msg dsply
c enddo
c callp close(fd)
c eval Msg = 'Press ENTER to continue'
c dsply Msg
C*------------------------
CSR endsr
Open for Reading: Use open() with O_RDONLY (read-only).
Read Lines: Use readline() in a DOW loop to read each line into line. Check if readline() returns >= 0 to continue.
Display Lines: Assign line to msg and use DSPLY to show the first 52 bytes.
Close the File: Call close(fd) after reading.
Practical Tips
Always check file descriptor (fd) after open().
If fd < 0, handle the error with die().
Use %trimr() to remove trailing spaces before calculating line length.
Ensure the IFS path (/ifstest/) exists. Create it with MKDIR if needed.
DSPLY limits output to 52 bytes. Plan your display logic accordingly.
Grab the latest version of the source code from here nick.litten.public