Friday, 12 December 2008

INPUT AND OUTPUT VERBS

This verbs OPEN, READ, WRITE and CLOSE are available for input output operations.
OPEN:
When a READ or a write operation is performed on a file, it must be open. The opening of a file may be done with the help of the OPEN verb. With the OPEN verb it must also be indicated whether the file should be opened as an input file or output file. If it is an input file, only reading is possible, whereas in the case of an output file, only writing is possible. A file once opened remains open until it is closed by a CLOSE statement. The OPEN statement in its simple form is as follows:

Example – 1
OPEN INPUT TRANSACTION, OLD-MASTER OUTPUT NEW-MASTER.
The example shows that there are two input files named TRANSACTION and OLD-MASTER and one output file called, NEW-MASTER. All these files are opened and these are ready for reading and writing.
READ:
The purpose of this verb is to make available the next logical record from an input file.
The first time the READ statement is executed, the first record to the file will be read into the record area described in the FILE SECTION of the DATA DIVISION. The next time the READ statement is executed, the second record will be read in the same area. In this way each time READ statement is executed the successive records will be read in the same area. Thus a time will come when there will be no more records in the file. In that case the statements following the AT END clause will be executed.
The format of the READ statement is

The INTO option may be used first to read a record into the record area for the file and then to get it moved to the area indicated identifier-1. READ with the INTO option is a combination of a READ followed by a MOVE statement.
Example:
READ TRANSACTION-RECORD AT END GO TO PARA-END.
The next record from the TRANSACTION file will be read if it is available. If the file does not contain any more records, the control will be transferred to the paragraph names PARA-END.
WRITE:
The WRITE verb releases a record onto an output file. The syntax of the WRITE statement can be different depending on the output device and medium used. The verb as described here can be used only to print results on a continuous stationery through a line printer. The form of the WRITE statement in such a case is

In the READ statement the file name is to be specified, whereas in the case of the WRITE statement it is required to mention the record name and not the file name.
The ADVANCING phrase is used to control the vertical positioning of each record at the time of printing on the stationery placed on the printer.
When the BEFORE phrase is used, the record is printed before the stationery is advanced, whereas the AFTER phrase may be used when the intention is to advance the stationery first and then to print the record.
If integer-1 or identifier-1 is mentioned, the stationery is advanced by the number of lines equal to the value of integer-1 or to the current value of identifier-1.
If the mnemonic-name is specified, the printer will be advanced to the carriage control channel declared for the mnemonic-name is the SPECIAL-NAMES paragraph.
Examples:
WRITE TRANS-RECORD AFTER ADVANCING 3 LINES.
This WRITE statement indicates that TRANS-RECORD is a record name of a file that has been assigned to PRINTER. The current position of the stationery will be advanced by 3 lines.

CLOSE:
When the processing of a file is over, the file may be closed. This is done with the help of the CLOSE verb.
The form of the CLOSE statement is
CLOSE file-name-1 [ , file-name-2 ] . . .
Once a file is closed, it is no longer available to the program. It should be opened again if the file is required subsequently.
Example:
CLOSE TRANSACTION, OLD-MASTER, NEW-MASTER, PRINT-FILE.
This statement will close all the four file – TRANSADCTION, OLD-MASTER, NEW-MASTER AND PRINT-FILE.

No comments:

Post a Comment