sign in
 
   
 
 
 
   
  COBOL TUTORIAL FOR BCA STUDENTS OF M G UNIVERSITY  
  UNIT 4: PROCEDURE DIVISION . . .  
   
     
  INPUT AND OUTPUT VERB  
 

INPUT and OUTPUT verbs are used to read data into the memory from some input medium and to write the processed results from the memory onto some output medium.

 
   
  OPEN VERB  
 

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 be also 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:


 
 

OPEN INPUT TRANSACTION, OLD-MASTER OUTPUT NEW-MASTER.

 
 

The example shows that there are two input files named TRANSACTION and OLDMASTER and one output file called NEW-MASTER. All these files are opened and these are ready for reading or writing.

 
  OPEN INPUT MARK-FILE.
OPEN OUTPUT RESULT-FILE.
 
 

The first OPEN statement opens the MARK-FILE in input mode and the file is ready for reading. The next statement makes the RESULT-FILE ready for writing. There may be several OPEN statements in a program.

 
   
  CLOSE VERB  
 

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:

 
   
 

The file must be open when a close statement can be executed. Once a file is closed, it is no longer available to the program. It should be opened again if the file is required subsequently. It may be noted that unlike the OPEN statement, the nature of the use of the file (input and output) should not be mentioned in the CLOSE statement.

Example:

 
  CLOSE TRANSACTION, OLD-MASTER, NEW-MASTER, PRINT-FILE.  
 

This statement will close all the four files – TRANSACTION, OLD-MASTER, NEWMASTER and PRINT-FILE.