1 / 12

Topic objectives

Learn basic sequential file processing in COBOL including open, read, process, validate, compute, write, and close file operations. Explore batch program design patterns for efficient file handling.

menke
Download Presentation

Topic objectives

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Topic objectives This topic covers basic (simple) sequential file processing – patterns: • Open files • Read an initial record from the input file • Process all records until end-of-input-file • Edit and validate data • Compute totals, subtotals and accumulators • Write output records • Read the next input file record • Close files and end the job • In subsequent topics of this course, we will dive much more deeply into file handling, as a majority of COBOL batch processing depends on concepts and coding patterns that are variations of the above • We will also touch on the basic batch program design patterns you will want to use going forward in this course and later in your production COBOL work By the end of this chapter you will be able to: • OPEN, READ, WRITE and CLOSE files • Loop through and input file, processing all of the records until completed

  2. Sequential File Processing • Sequential File Processing consists of a well-documented set of processing – or a pattern that you will see in many program requirements • You read one or more input files until: • Your business logic requirements are fulfilled • End-of-File • While reading files you typically: • Edit or evaluate data • Add numeric values to total and sub-total fields – perform business calculations • Assign (MOVE) values • WRITE output record(s) – either to an output file, report or both • You must be cognizant of: • Bad or invalid data (and know what to do about it when it shows up in your fields) • Empty input files • READ/WRITE I/O errors that may occur • After reaching end-of-file you will typically: • WRITE a final output record, with summary computed values • CLOSE all files • DISPLAY a successful end-of-job message Output Report Input File Note - more than likely NOT a tape Device  COBOL Program Business Logic Output File Record Buffer Record Buffer

  3. Sequential File Processing Pattern – Simple Batch Design Pattern This first batch application pattern "Process One Input File" – consists of the following pattern • Perform an initialization routine • Initialize values in Working-Storage • OPEN the files – for either input or output • PERFORM a "priming" input-record read – an initial read statement that simplifies: • Empty input-file-processing problems • Reading past end-of-file logic problems • Perform a process-the-file routine, until end-of-input-file • Validate the data – using the conditional logic statements learned in this unit • Move the data – using the assignment statements learned in this unit • Do computations, calculations, etc. – using the COBOL math statements • Write the record • Read the next input record • Perform an end-of-job routine • Complete final computations • WRITE final output record with final computations • CLOSE all files and display an end-of-job message on the Console

  4. External Device External File File I/O Review – OPEN … ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. SELECT INTFILENAME ASSIGN TO … … DATA DIVISION. FILE SECTION FD INTFILENAME… 01 OUT-REC. … PROCEDURE DIVISION. … OPEN INPUT FILE1, FILE2 OUTPUT FILE3, FILE4. … Record Buffer Recall the following: • SELECT/ASSIGN connects your internal (logical) filename with external (physical) file-spec • FILE SECTION is required for each SELECT/ASSIGN file • OPEN references the logical (internal) filename • Can OPEN multiple files with one statement (as shown) • Note carefully the syntax for OPEN See Slide Notes, for additional learning content

  5. External Device External File File I/O Review – READ INTO ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. SELECT INTFILENAME ASSIGN TO … DATA DIVISION. FILE SECTION FD INTFILENAME … 01 IN-REC. WORKING-STORAGE SECTION 01 IN-REC-WS. 01 END-OF-FILE-FLAG PIC X. PROCEDURE DIVISION. OPEN INPUT INTFILENAME. READ INTFILENAME INTO IN-REC AT END MOVE ‘Y’ to END-OF-FILE-FLAG. … F I L E R E A D  Record Buffer Recall the following: • File must be OPEN before you try to READ from it • READ retrieves each record from an external file: - Into the record specified in your FILE SECTION * Note that the DATA DIVISION's FILE SECTION is sometimes referred to as an I/O "buffer" - Into WORKING-STORAGE - if you've coded READ INTO • AT END condition occurs after the last record is READ from the file - If you attempt to read past end-of-file your program will ABEND See Slide Notes, for additional learning content

  6. External Device External File File I/O Review – WRITE FROM ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. SELECT OUTFILENAME ASSIGN TO … DATA DIVISION. FILE SECTION FD OUTFILENAME… 01 OUT-REC. WORKING-STORAGE SECTION 01 OUT-REC-WS. PROCEDURE DIVISION. OPEN OUTPUT OUTFILENAME. WRITE OUT-REC FROM OUT-REC-WS. … W R I T E O P E R A T I O N Record Buffer Recall the following: • File must be OPEN before you try to WRITE a record to it - An ABEND condition occurs, if try to write to an un-opened file • WRITE creates a new record at the end of a sequential/external file: - From the record specified in your FILE SECTION - From WORKING-STORAGE - if you've coded WRITE FROM • Close opened output files before your program ends See Slide Notes, for additional learning content

  7. External Device External File File I/O Review – CLOSE ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. SELECT INTFILENAME ASSIGN TO … … DATA DIVISION. FILE SECTION FD INTFILENAME… 01 OUT-REC. … PROCEDURE DIVISION. … CLOSE FILE1, FILE2, FILE3, FILE4. … F I L E C L O S E Record Buffer Recall the following: • CLOSE every opened file - Input file - Output file • Can CLOSE multiple files with one statement (as shown) • Can re-OPEN a file after closing • Note carefully the syntax for CLOSE See Slide Notes, for additional learning content

  8. PROCEDURE DIVISION. Do Init-Routine Do Process-Files until No-More-Data Do End-of-Job-Routine GOBACK. Init-Routine. Open files READ Input-File Process-Files. Validate input data Perform calculations and sub-totals Move data to output Record Write Output-Record READ Input-File End-of-Job-Routine. Final computations Move Data to output Record Write output record Close Files Sequential File Processing Pattern – Pseudo-Code • In a subsequent unit of this course we will cover the concept of design and programming patterns with COBOL • For now, here is your first simple, sequential file-handling pattern, in pseudo-code. • Study the control-flow to understand the paragraph PERFORM process, and purpose of each statement block • Tie this back to the code you the previous slide in this unit. • What is not part of this processing pattern are the actual details of the: • Data Validation • Calculations and computations … which can be extremely complex and will most likely take up the majority percentage of your coding cycles. • There are other algorithms that can do the same sequential process, but we feel this is a simple, easy-to-read/maintain/debug-support/and extend approach

  9. Putting it All Together - A Complete Example of a Sequential File Processing Program • Read through (study) the code in this sample small sequential file processing program. • Trace the concepts, COBOL syntax and statement semantics learned in this, and the previous Unit to the actual code in this program. • Feel free to refer-back to your slides, the course supplemental text or your notes – if necessary Select External File Assign to COBOL (internal) file name FILE SECTION FDs Matching the SELECT/ASSIGN Clauses for each external file Note – this example continues over the next three slides

  10. A Complete Example of a Sequential File Processing Program – WORKING-STORAGE SECTION File Status fields Automatically updated By COBOL and the O.S. A final-totals output record Input and Output records You will often see these coded in WORKING-STORAGE because they are easier to find in a System log file (called a DUMP file) if your program files (ABENDS) when executing on the mainframe Holding fields for intermediate result calculations and values

  11. A Complete Example of a Sequential File Processing Program – PROCEDURE DIVISION E D I T S • Processing • Calculations • Computations • Our Sequential File Pattern – in COBOL • Read the code line-by-line (as if you were debugging it – • In other words, if there's a PERFORM – jump down to the • Performed paragraph. When finished jump back, etc.) • Note the use of: • PERFORM THRU • AT END/GO TO "EXIT" • PERFORM UNTIL • For each record in the input file, do 100-MAINLINE M O V E Write the current record Read the next record in the file

  12. A Complete Example of a Sequential File Processing Program – No More Records in the File • When there are no more records left to process • Do final calculations • Move the fields to final output record • WRITE • CLOSE all files • Display a message on the console

More Related