Summary -

In this topic, we described about the File Declaration with detailed example.

Any file that requires processing through COBOL application program, it should declare with all its characteristics in the program. Application program use these characteristics to know about the file like what kind of file and what kind of operations to perform etc.

Once the program knows about the file, program process the file using file handling statements. File declaration has two parts -

IFile I-O Definition Contains the complete information about the files used in the program.
Defines in FILE-CONTROL in INPUT-OUTPUT-SECTION of ENVIRONMENT DIVISION.
File Structure Definition Defines the structure of data files.
Defines in FILE SECTION of DATA DIVISION.

Practical Example -

Code:


  IDENTIFICATION DIVISION.                                        
  PROGRAM-ID. SEQFILE.                                             
  ENVIRONMENT DIVISION.                                           
  INPUT-OUTPUT SECTION.                                            
  FILE-CONTROL.                                                    
      SELECT FILE1 ASSIGN TO DISK1.                                
      ORGANIZATION IS SEQUENTIAL                                  
      ACCESS MODE IS SEQUENTIAL                                    
      FILE STATUS IS WS-FS.                                        
  DATA DIVISION.                                                  
  FILE SECTION.                                                    
      RECORD CONTAINS 80 CHARACTERS.                               
      BLOCK CONTAINS 800 CHARACTERS.                               
      RECORDING MODE IS F.                                        
      DATA RECORD IS STD-REC.                                     
  FD  FILE1.                                                      
  01 STD-REC.                                                     
      02 STD-NO          PIC 9(03).                                
      02 STD-NAME        PIC X(20).                                
      02 STD-GENDER      PIC X(07).                                
      02 FILLER          PIC X(50).                                
  WORKING-STORAGE SECTION.                                         
  77 WS-FS               PIC 9(02).                                
  01 WS-EOF-SW           PIC X(01) VALUE 'N'.                      
     88 EOF-SW           VALUE 'Y'.                                
     88 NOT-EOF-SW       VALUE 'N'.                                
  PROCEDURE DIVISION.                                              
      DISPLAY 'SEQUENTIAL FILE READING...'.                        
      OPEN INPUT FILE1.                                           
      PERFORM UNTIL EOF-SW                                         
         READ FILE1                                                
         AT END MOVE 'Y' TO WS-EOF-SW                              
         DISPLAY 'RECORD READ : ' STD-REC                          
      END-PERFORM.                                                 
      CLOSE FILE1.                                                 
      DISPLAY STD-REC.                                            
      STOP RUN.