In this section, we will explore how to perform file input and output (I/O) operations in REXX. File I/O is a crucial aspect of programming as it allows you to read from and write to files, enabling data persistence and manipulation.

Key Concepts

  1. Opening Files: Learn how to open files for reading, writing, or appending.
  2. Reading from Files: Understand how to read data from files.
  3. Writing to Files: Learn how to write data to files.
  4. Closing Files: Ensure files are properly closed after operations.
  5. Error Handling: Handle errors that may occur during file operations.

Opening Files

To perform any file operation, you first need to open the file. In REXX, you use the OPEN function to open a file.

Syntax

CALL OPEN fileName, mode
  • fileName: The name of the file you want to open.
  • mode: The mode in which you want to open the file. Common modes include:
    • 'r' for reading
    • 'w' for writing
    • 'a' for appending

Example

/* Open a file for reading */
CALL OPEN 'example.txt', 'r'

Reading from Files

Once a file is opened for reading, you can use the LINEIN function to read data from it.

Syntax

data = LINEIN(fileName)
  • fileName: The name of the file from which you want to read.

Example

/* Read a line from the file */
data = LINEIN('example.txt')
SAY data

Writing to Files

To write data to a file, you use the LINEOUT function.

Syntax

CALL LINEOUT fileName, data
  • fileName: The name of the file to which you want to write.
  • data: The data you want to write to the file.

Example

/* Write a line to the file */
CALL LINEOUT 'example.txt', 'Hello, World!'

Closing Files

After performing file operations, it is important to close the file using the CLOSE function.

Syntax

CALL CLOSE fileName
  • fileName: The name of the file you want to close.

Example

/* Close the file */
CALL CLOSE 'example.txt'

Error Handling

File operations can fail for various reasons, such as the file not existing or lacking permissions. It is important to handle these errors gracefully.

Example

/* Open a file for reading with error handling */
IF OPEN('example.txt', 'r') \= 0 THEN
  SAY 'Error: Unable to open file for reading'
ELSE
  DO
    /* Read and process the file */
    data = LINEIN('example.txt')
    SAY data
    CALL CLOSE 'example.txt'
  END

Practical Exercise

Exercise 1: Reading from a File

  1. Create a text file named input.txt with the following content:
    Line 1
    Line 2
    Line 3
    
  2. Write a REXX script to read and display each line from the file.

Solution

/* Open the file for reading */
IF OPEN('input.txt', 'r') \= 0 THEN
  SAY 'Error: Unable to open file for reading'
ELSE
  DO
    /* Read and display each line */
    DO WHILE LINES('input.txt') > 0
      line = LINEIN('input.txt')
      SAY line
    END
    /* Close the file */
    CALL CLOSE 'input.txt'
  END

Exercise 2: Writing to a File

  1. Write a REXX script to write the following lines to a file named output.txt:
    Hello, World!
    This is a test.
    

Solution

/* Open the file for writing */
CALL OPEN 'output.txt', 'w'

/* Write lines to the file */
CALL LINEOUT 'output.txt', 'Hello, World!'
CALL LINEOUT 'output.txt', 'This is a test.'

/* Close the file */
CALL CLOSE 'output.txt'

Summary

In this section, you learned how to perform basic file I/O operations in REXX, including opening, reading, writing, and closing files. You also learned how to handle errors that may occur during these operations. These skills are essential for any programmer looking to work with file data in REXX.

© Copyright 2024. All rights reserved