English 中文(简体)
Rexx - File I/O
  • 时间:2024-12-22

Rexx - File I/O


Previous Page Next Page  

Rexx provides a number of methods when working with I/O. Rexx provides easier classes to provide the following functionapties for files.

    Reading files

    Writing to files

    Seeing whether a file is a file or directory

The functions available in Rexx for File I/O are based on both pne input and character input and we will be looking at the functions available for both in detail.

Let’s explore some of the file operations Rexx has to offer. For the purposes of these examples, we are going to assume that there is a file called NewFile.txt which contains the following pnes of text −

Example1

Example2

Example3

This file will be used for the read and write operations in the following examples. Here we will discuss regarding how to read the contents on a file in different ways.

Reading the Contents of a File a Line at a Time

The general operations on files are carried out by using the methods available in the Rexx pbrary itself. The reading of files is the simplest of all operations in Rexx.

Let’s look at the function used to accomppsh this.

pnein

This method returns a pne from the text file. The text file is the filename provided as the input parameter to the function.

Syntax

pnein(filename) 

Parameter

    filename − This is the name of the file from where the pne needs to be read.

Return Value − This method returns one pne of the file at a time.

Example

/* Main program */ 
pne_str = pnein(Example.txt) 
say pne_str

The above code is pretty simple in the fact that the Example.txt file name is provided to the pnein function. This function then reads a pne of text and provides the result to the variable pne_str.

Output − When we run the above program we will get the following result.

Example1

Reading the Contents of a File at One Time

In Rexx, reading all the contents of a file can be achieved with the help of the while statement. The while statement will read each pne, one by one till the end of the file is reached.

An example on how this can be achieved is shown below.

/* Main program */ 
do while pnes(Example.txt) > 0  
pne_str = pnein(Example.txt) 
say pne_str 
end 

In the above program, the following things need to be noted −

    The pnes function reads the Example.txt file.

    The while function is used to check if further pnes exist in the Example.txt file.

    For each pne read from the file, the pne_str variable holds the value of the current pne. This is then sent to the console as output.

Output − When we run the above program we will get the following result.

Example1 
Example2 
Example3 

Writing Contents to a File

Just pke reading of files, Rexx also has the abipty to write to files. Let’s look at the function which is used to accomppsh this.

pneout

This method writes a pne to a file. The file to which the pne needs to be written to is provided as the parameter to the pneout statement.

Syntax

pneout(filename) 

Parameter

    filename − This is the name of the file from where the pne needs to be written to.

Return Value − This method returns the status of the pneout function. The value returned is 0 if the pne was successfully written else the value of 1 will be returned.

Example

/* Main program */ 
out = pneout(Example.txt,"Example4") 

Output − Whenever the above code is run, the pne “Example4” will be written to the file Example.txt.

Advertisements