Cobol Program to Read a File and Write to Another File

COBOL - File Handling Verbs


File handling verbs are used to perform diverse operations on files. Post-obit are the file handling verbs −

  • Open
  • Read
  • Write
  • Rewrite
  • Delete
  • Start
  • Close

Open Verb

Open is the first file operation that must be performed. If Open up is successful, so only further operations are possible on a file. Only after opening a file, the variables in the file construction are available for processing. FILE STATUS variable is updated after each file operation.

Syntax

OPEN "mode" file-name.        

Here, file-name is string literal, which you will use to proper name your file. A file can be opened in the following modes −

Sr.No. Mode & Clarification
1

Input

Input fashion is used for existing files. In this way, nosotros tin can only read the file, no other operations are allowed on the file.

2

Output

Output mode is used to insert records in files. If a sequential file is used and the file is property some records, and so the existing records volition be deleted first and then new records will be inserted in the file. It volition not happen and so in instance of an indexed file or a relative file.

3

Extend

Extend mode is used to append records in a sequential file. In this mode, records are inserted at the end. If file access way is Random or Dynamic, then extend way cannot exist used.

4

I-O

Input-Output manner is used to read and rewrite the records of a file.

Read Verb

Read verb is used to read the file records. The function of read is to fetch records from a file. At each read verb, only one record tin be read into the file structure. To perform a read operation, open the file in INPUT or I-O mode. At each read statement, the file pointer is incremented and hence the successive records are read.

Syntax

Following is the syntax to read the records when the file admission mode is sequential −

READ file-name Next RECORD INTO ws-file-structure    AT END DISPLAY 'Terminate of File'    NOT AT Cease DISPLAY 'Record Details:' ws-file-structure END-READ.        

Post-obit are the parameters used −

  • NEXT RECORD is optional and is specified when an indexed sequential file is being read sequentially.

  • INTO clause is optional. ws-file-structure is defined in the WorkingStorage Section to get the values from the READ statement.

  • AT Stop status becomes True when the terminate of file is reached.

Instance − The post-obit example reads an existing file using line sequential arrangement. This plan can be compiled and executed using Live Demo option where information technology will display all the records present in the file.

IDENTIFICATION Sectionalization. Programme-ID. Hello.  ENVIRONMENT Division.    INPUT-OUTPUT Section.       FILE-CONTROL.       SELECT STUDENT ASSIGN TO 'input.txt'       System IS LINE SEQUENTIAL.              Information DIVISION.    FILE SECTION.    FD STUDENT.    01 STUDENT-FILE.       05 Pupil-ID PIC 9(v).       05 NAME Pic A(25).     WORKING-STORAGE SECTION.    01 WS-STUDENT.       05 WS-STUDENT-ID Film 9(5).       05 WS-NAME Picture show A(25).    01 WS-EOF PIC A(1).   PROCEDURE DIVISION.    Open INPUT STUDENT.       PERFORM UNTIL WS-EOF='Y'          READ Pupil INTO WS-STUDENT             AT END MOVE 'Y' TO WS-EOF             Non AT Finish Display WS-STUDENT          Cease-READ       Finish-PERFORM.    CLOSE Pupil. STOP RUN.        

Suppose the input file data available in the input.txt file contains the post-obit −

20003 Mohtashim Chiliad. 20004 Nishant Malik 20005 Amitabh Bachhan        

When you compile and execute the higher up program, it produces the following consequence −

20003 Mohtashim M.             20004 Nishant Malik            20005 Amitabh Bachhan        

Syntax

Following is the syntax to write a record when the file admission mode is random −

READ file-name RECORD INTO ws-file-construction    KEY IS rec-key    INVALID Central Brandish 'Invalid Primal'    Non INVALID Cardinal DISPLAY 'Record Details: ' ws-file-structure END-READ.        

Example − The following instance reads an existing file using indexed organization. This programme can exist compiled and executed using JCL on Mainframes where it volition display all the records present in the file. On Mainframes server, we practice not utilize text files; instead we employ PS files.

Let's assume that the file present on Mainframes have aforementioned content as input.txt file in the above example.

IDENTIFICATION Segmentation. PROGRAM-ID. HELLO.  Surround Partition.    INPUT-OUTPUT SECTION.    FILE-CONTROL.    SELECT Student ASSIGN TO IN1       Organisation IS INDEXED       ACCESS IS RANDOM       RECORD KEY IS Educatee-ID       FILE STATUS IS FS.  DATA Sectionalisation.    FILE Section.    FD STUDENT.       01 Pupil-FILE.       05 Student-ID PIC nine(five).       05 NAME PIC A(25).          WORKING-STORAGE SECTION.    01 WS-Pupil.       05 WS-Educatee-ID PIC ix(5).       05 WS-Proper noun PIC A(25).  Process Partition.    OPEN INPUT Student.       Motility 20005 TO Student-ID.              READ STUDENT RECORD INTO WS-Educatee-FILE          Fundamental IS Pupil-ID          INVALID Fundamental Brandish 'Invalid Key'          Non INVALID KEY DISPLAY WS-Student-FILE       END-READ.           Close STUDENT. STOP RUN.        

JCL to execute the to a higher place COBOL program −

//SAMPLE Job(TESTJCL,XXXXXX),Grade = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO //IN1 DD DSN = STUDENT-FILE-NAME,DISP=SHR        

When you compile and execute the above program, it produces the following result −

20005 Amitabh Bachhan        

Write Verb

Write verb is used to insert records in a file. One time the tape is written, information technology is no longer available in the record buffer. Before inserting records into the file, move the values into the tape buffer and so perform write verb.

Write argument can be used with FROM option to directly write records from the working storage variables. From is an optional clause. If the access manner is sequential, then to write a tape, the file must open in Output mode or Extend manner. If the access style is random or dynamic, so to write a record, the file must open in Output mode or I-O style.

Syntax

Following is the syntax to read a record when the file organization is sequential −

WRITE record-buffer [FROM ws-file-structure] END-WRITE.        

Following is the syntax to read a record when the file arrangement is indexed or relative −

WRITE tape-buffer [FROM ws-file-construction]    INVALID KEY DISPLAY 'Invalid Primal'    Not INVALID Key Display 'Record Inserted' End-WRITE.        

Example − The following case shows how to insert a new record in a new file when the organization is sequential.

IDENTIFICATION Partition. Program-ID. Howdy.  ENVIRONMENT Sectionalisation.    INPUT-OUTPUT SECTION.    FILE-CONTROL.    SELECT Educatee ASSIGN TO OUT1       System IS SEQUENTIAL       ACCESS IS SEQUENTIAL       FILE Condition IS FS.  Data DIVISION.    FILE Department.    FD STUDENT    01 STUDENT-FILE.       05 STUDENT-ID Movie nine(5).       05 NAME PIC A(25).       05 CLASS Picture X(3).     WORKING-STORAGE SECTION.    01 WS-Student.       05 WS-Pupil-ID Picture show 9(5).       05 WS-Name PIC A(25).       05 WS-Class PIC X(3).  Process Partition.    Open up EXTEND Student.       Motion 1000 TO Educatee-ID.       Movement 'Tim' TO NAME.       MOVE 'ten' TO CLASS.       WRITE STUDENT-FILE       Finish-WRITE.	    Close STUDENT. Cease RUN.        

JCL to execute the above COBOL program −

//SAMPLE JOB(TESTJCL,XXXXXX),Form = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO //OUT1 DD DSN = OUTPUT-FILE-Proper noun,DISP = (NEW,Catalog,DELETE)        

When you compile and execute the above program, it will add together a new record to the output file.

chiliad Tim         10        

Rewrite Verb

Rewrite verb is used to update the records. File should be opened in I-O fashion for rewrite operations. It can be used just after a successful Read performance. Rewrite verb overwrites the last record read.

Syntax

Post-obit is the syntax to read a tape when the file arrangement is sequential −

REWRITE tape-buffer [FROM ws-file-structure] Finish-REWRITE.        

Post-obit is the syntax to read a record when the file organization is indexed or relative −

REWRITE tape-buffer [FROM ws-file-structure]    INVALID KEY Brandish 'Invalid Central'    NOT INVALID KEY Display 'Record Updated' END-REWRITE.        

Example − The following instance shows how to update an existing tape which we have inserted in the previous Write step −

IDENTIFICATION DIVISION. PROGRAM-ID. HELLO.  Environment DIVISION.    INPUT-OUTPUT Department.    FILE-CONTROL.    SELECT Pupil ASSIGN TO IN1       ORGANIZATION IS INDEXED       ACCESS IS RANDOM       RECORD Primal IS Student-ID       FILE Condition IS FS.  DATA DIVISION.    FILE SECTION.    FD STUDENT    01 Pupil-FILE.       05 Student-ID PIC 9(four).       05 Proper name Film A(12).       05 CLASS PIC Ten(3).           WORKING-STORAGE SECTION.    01 WS-Student.       05 WS-Educatee-ID Flick 9(5).       05 WS-NAME PIC A(25).       05 WS-CLASS Film X(3).  PROCEDURE DIVISION.    OPEN I-O Pupil.    MOVE '1000' TO STUDENT-ID.       READ STUDENT       Central IS Pupil-ID       INVALID KEY DISPLAY 'Key IS Non EXISTING'    Finish-READ.       MOVE 'Tim Dumais' TO Proper name.    REWRITE STUDENT-FILE    END-REWRITE.    CLOSE STUDENT. End RUN.        

JCL to execute the to a higher place COBOL programme −

//SAMPLE Job(TESTJCL,XXXXXX),Class = A,MSGCLASS = C //STEP1 EXEC PGM = How-do-you-do //IN1 DD DSN = OUTPUT-FILE-Proper name,DISP = SHR        

When you compile and execute the to a higher place program, it will update the record −

1000 Tim Dumais  x        

Delete Verb

Delete verb can be performed merely on indexed and relative files. The file must be opened in I-O mode. In sequential file organization, records cannot be deleted. The record concluding read by the Read argument is deleted in example of sequential access mode. In random admission manner, specify the record primal and and so perform the Delete operation.

Syntax

Following is the syntax to delete a record −

DELETE file-name Record    INVALID Primal Brandish 'Invalid Key'    Non INVALID KEY DISPLAY 'Record Deleted' END-DELETE.        

Example − to delete an existing record −

IDENTIFICATION DIVISION. PROGRAM-ID. HELLO.  Surroundings DIVISION.    INPUT-OUTPUT SECTION.    FILE-CONTROL.    SELECT STUDENT ASSIGN TO OUT1       ORGANIZATION IS INDEXED       ACCESS IS RANDOM       Tape Fundamental IS Pupil-ID       FILE Condition IS FS.  Data DIVISION.    FILE SECTION.    FD STUDENT    01 STUDENT-FILE.       05 Educatee-ID Motion-picture show 9(four).       05 Name Motion-picture show A(12).       05 Form PIC 10(3).    WORKING-STORAGE SECTION.    01 WS-STUDENT.       05 WS-STUDENT-ID PIC 9(5).       05 WS-Proper name Moving picture A(25).       05 WS-CLASS PIC Ten(3).  PROCEDURE Partitioning.    OPEN I-O Educatee.    MOVE '1000' TO Student-ID.        DELETE STUDENT Tape       INVALID Key Brandish 'Invalid Key'       NOT INVALID KEY Brandish 'Record Deleted'    Cease-DELETE.        CLOSE STUDENT. STOP RUN.        

JCL to execute the above COBOL program −

//SAMPLE Chore(TESTJCL,XXXXXX),Course = A,MSGCLASS = C //STEP1 EXEC PGM = HELLO //OUT1 DD DSN = OUTPUT-FILE-Name,DISP = SHR        

When you compile and execute the above program, information technology produces the following upshot −

Record Deleted        

Start Verb

Start verb tin be performed only on indexed and relative files. Information technology is used to place the file pointer at a specific record. The access way must be sequential or dynamic. File must be opened in I-O or Input fashion.

Syntax

Following is the syntax to place the pointer at a specific record −

Showtime file-name KEY IS [=, >, <, Non, <= or >=] rec-key    INVALID Central Brandish 'Invalid Key'    Not INVALID Key Brandish 'File Pointer Updated' Stop-Beginning.        

Close Verb

Close verb is used to shut a file. After performing Close operation, the variables in the file structure will not exist available for processing. The link between programme and file is lost.

Syntax

Following is the syntax to close a file −

Close file-name.        

Useful Video Courses


COBOL Online Training

Video

kellyassarat76.blogspot.com

Source: https://www.tutorialspoint.com/cobol/cobol_file_handling_verbs.htm

0 Response to "Cobol Program to Read a File and Write to Another File"

إرسال تعليق

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel