|
1
|
- Jon Sayles, IBM Software Group, Rational EcoSystems Team
|
|
2
|
- © Copyright IBM Corporation 2007,2008, 2009. All rights reserved.
- The information contained in these materials is provided for
informational purposes only, and is provided AS IS without warranty of
any kind, express or implied.
IBM shall not be responsible for any damages arising out of the
use of, or otherwise related to, these materials. Nothing contained in these materials
is intended to, nor shall have the effect of, creating any warranties
or representations from IBM or its suppliers or licensors, or altering
the terms and conditions of the applicable license agreement governing the use of IBM software.
References in these materials to IBM products, programs, or services do
not imply that they will be available in all countries in which IBM
operates.
- This information is based on current IBM product plans and strategy,
which are subject to change by IBM without notice. Product release
dates and/or capabilities referenced in these materials may change at
any time at IBM’s sole discretion based on market opportunities or
other factors, and are not intended to be a commitment to future
product or feature availability in any way.
- IBM, the IBM logo, the on-demand business logo, Rational, the Rational
logo, and other IBM Rational products and services are trademarks or
registered trademarks of the International Business Machines
Corporation, in the United States, other countries or both. Other
company, product, or service names may be trademarks or service marks
of others.
|
|
3
|
- Thanks to the following individuals, for assisting with this course:
- David Myers/IBM,
- Ka Yin Lam/IBM
- John Fenyar
- Wilbert Kho/IBM
- Steven Wilcenski/Sabre Systems Inc.
|
|
4
|
- Course Name: COBOL Foundation
Training - with RDz
- Course Description: Learn the
COBOL language, RDz and learn z/OS terms, concepts and development
skills in this course.
- Pre-requisites: Some experience in a 3rd or 4th Generation Language is
expected. SQL is also
recommended.
- Course Length: 10 days
- Topics (Agenda)
- Getting Started - installing and configuring RDz - and the course materials, and using Eclipse to edit
COBOL
- COBOL General Language Rules
- Basic COBOL Statements
- Advanced record and table handling
- Debugging Programs - Note: Deep dive on using RDz for common COBOL
programming errors (001, 0C4, 0C7, infinite loops, fall-thru, etc.)
- Input/Output and Report Writing Patterns
- Sequential File Match/Merge Patterns
- COBOL Subprograms and the Linkage Section
- Structured Programming Concepts and Coding Patterns
- Advanced Character Manipulation, COBOL Intrinsic Functions, Date and
Time coding patterns, and Language Environment calls
- OS/390 Concepts and JCL
- Compile/Link & Run Procs on the mainframe
- Indexed file Coding Patterns
- Sort/Merge and Master File Update Coding Patterns
- Accessing DB2 Data and Stored Procedures
- COBOL in the Real World:
- CICS - lecture only
- IMS (DL/I and TM) - ditto
- Batch processing - ditto
- Java calling COBOL
- COBOL and XML Statements
- SOA and COBOL - creating and calling Web Services
- Web 2.0 using Rich UI
|
|
5
|
- Audience
- This course is designed for application developers who have programmed
in some language before, and who wish to learn COBOL.
- Prerequisites
- This course assumes that the student has the basic knowledge of IS
technologies, data processing, software and have programmed for at
least two or more years in a language such as: Java, VB, RPG, PL/1,
Pascal, or some 4th Generation Language or tool.
- Knowledge of SQL (Structured Query Language) for database access is
assumed as well.
- Basic PC and mouse-driven development skills is also assumed.
- Finally, it is assumed that you know a little bit about COBOL and
mainframe technologies – some of which is covered in the “Getting
Started” and RDz Editing sections
|
|
6
|
|
|
7
|
- After completing this topic, you should be able to:
- Describe the areas of COBOL structured coding – and what kind of COBOL statements go into each
- Define the four DIVISIONs of a COBOL program
- List the kinds of alphanumeric characters that can be used in coding
COBOL programs
|
|
8
|
- COBOL code is written using:
- Alphanumeric characters:
- The following special characters:
- + - *
/ = $
: . , ;
“ ‘ (
) < >
- Upper and lower case characters are equivalent (case insensitive),
except when they appear in a character string
- On the mainframe, the character set – and collating sequence is EBCDIC
- On the PC and on AIX systems, the character set and collating sequence
is ASCII
- The differences between EBCDIC manifest themselves at run-time in:
- Alphanumeric sort routines and operations
- IBM built-in functions that process sorted data (Median, etc.)
|
|
9
|
- COBOL program development uses a fixed-column or fixed-position coding
style:
- Statements must be coded within specified column-numbered area
boundaries
- Columns 1 è 6 contain an
optional (rarely used) sequence number for ordering statements
- Column 7 indicates either:
- COBOL comment line – by typing an asterisk * in column 7
- Multi-line continuation of long literal values - by typing a dash – in
column 7
- Columns 8 è 11 are called
the “A” or area – with column 8 termed the “A” margin. Columns 8 è 11 are reserved for:
- Division statements
- Procedure names
- Certain types of variable declarations
- Columns 12 è 72 are called
the “B” area – you code all of your procedural (business) logic here, as
well as the rest of the variable declarations
- Columns 73 è 80 are used by
certain Source Code Management Systems for identifying line changes in
versions.
|
|
10
|
- IDENTIFICATION DIVISION, which identifies the program with a name and,
if optionally gives other identifying information.
- ENVIRONMENT DIVISION, where you describe the aspects of your program
that depend on the computing environment.
- DATA DIVISION, where the characteristics of your data are defined
- PROCEDURE DIVISION, where the instructions related to your business
logic are specified.
- The PROCEDURE DIVISION of a program is sub-divided into sections
or paragraphs, which contain
executable COBOL sentences and statements è
- The Divisions must be coded in the order described
- There are many optional statements and keywords in the Divisions
- Division and procedure (Paragraph and Section) names: Begin in the A
margin, must end with a period, and must be the only statement on that
line
- Your program ends when either a GOBACK or STOP RUN statement is executed
|
|
11
|
|
|
12
|
- After completing this topic, you should be able to:
- Describe the purpose of the IDENTIFICATION DIVISION
- List the required elements of it
- Code a syntactically correct IDENTIFICATION DIVISION
- List a few optional paragraphs and sections of the IDENTIFICATION
DIVISION
|
|
13
|
- IDENTIFICATION DIVISION.
- Program-ID. PROGRAM1.
- Author. Bill Hudson.
- Installation. Computing Labs Inc.
- Date-Written. 12/21/2009.
- Date-Compiled. 12/30/2009.
- Use the IDENTIFICATION DIVISION to name a program and optionally provide
other identifying information.
- Required statements:
- IDENTIFICATION DIVISION.
- PROGRAM-ID. PROGNAME.
- You can use the optional AUTHOR, INSTALLATION, DATE-WRITTEN, and DATE-COMPILED
paragraphs for descriptive information about a program.
- The data you enter in the DATE-COMPILED paragraph is replaced with the
latest compilation date.
|
|
14
|
- COBOL Program-ID names must follow these rules:
- For mainframe (z/OS) COBOL programs:
- Traditionally eight characters
- That restriction has been lifted in current language levels
- Name must start with an alphabetic letter
- Name can contain only alpha-numerics (letters and 0 – 9)
- For PC (“standard”) COBOL programs:
- Maximum of thirty characters
- Name can contain:
- Cannot start or end the Program-ID name with a dash
- ! Note: It is good
programming practice to add in-line comments to the top of your COBOL
program. Comments that document:
- The business/technical purpose of the program
- A program maintenance log
- Any unusual or complex coding patterns
|
|
15
|
- You can extend the PROGRAM-ID paragraph, with the following optional
keywords that compile programs to allow for extended functionality:
- RECURSIVE – allows a program to be reentrant and called recursively
- PROGRAM-ID MYPROG IS RECURSIVE.
- INITIAL – tells the compiler to reset variables to specified initial
values upon program execution
- PROGRAM-ID MYPROG IS INITIAL.
- CLASS-ID.
- Enterprise COBOL provides facilities for developing object-oriented
programs using the COBOL programming language.
- OO COBOL programs contain a CLASS-ID paragraph, for defining an OO
class, instead of PROGRAM-ID, which defines a structured program
- OO COBOL will be touched on later in this course, as it is considered
an “emerging z/Series technology” in most I/T installations
- See slide notes for additional information on OO COBOL
|
|
16
|
|
|
17
|
- After completing this topic, you should be able to:
- Describe the purpose of the ENVIRONMENT DIVISION
- List the required elements of it
- Code a syntactically correct ENVIRONMENT DIVISION
- List a few optional paragraphs and sections of the ENVIRONMENT DIVISION
|
|
18
|
- ENVIRONMENT DIVISION.
- CONFIGURATION SECTION.
- SOURCE-COMPUTER. IBM-3081.
- OBJECT-COMPUTER. IBM-3081.
- INPUT-OUTPUT SECTION.
- SELECT PRINTFILE ASSIGN TO UPDPRINT
- ORGANIZATION IS SEQUENTIAL.
- Use the ENVIRONMENT DIVISION to identify input and output files for the
program, and to describe aspects of the program that depend on the
computing platform.
- ENVIRONMENT DIVISION and its sections can be optional.
- That is, you will code them when you need to specify elements of your
program that connect to the operational environment your program runs
in (example. Reading and writing to external files)
- The ENVIRONMENT DIVISION has two sections:
- CONFIGURATION SECTION – Optional section, used to specify the
development and run:
- Source-Computer – the compiling computer.
- Object-Computer – the run-time computer
- INPUT-OUTPUT SECTION – Used to define external files used by the
program (files are considered part of your program’s “environment”)
- The SELECT/ASSIGN statement is covered on the next slide
- ! Note that: ENVIRONMENT
DIVISION, CONFIGURATION SECTION and
INPUT-OUTPUT SECTION are coded in the “A” area of your source
code
|
|
19
|
- The FILE-CONTROL part of the INPUT-OUTPUT SECTION identifies the
external files your program reads and writes using COBOL “logical” name
identifiers
- These logical file names in your
program, are mapped to a physical input/output storage device attached
to a mainframe (typically disk storage)
- By using Select/Assign in your program, you achieve Physical/Logical
data independence - so you won’t have to change your code:
- If you move the physical file to a different device
- If you point to an altogether different file entirely
- All your program cares about is the internal (logical) file name in
Select/Assign
- The system is responsible to find and attach the correct external
dataset
- Your program source…
- ENVIRONMENT DIVISION.
- INPUT-OUTPUT SECTION.
- FILE-CONTROL.
- SELECT INPUT ASSIGN TO INPUT1 (your
program’s internal COBOL filename)
- …
- The mainframe “job” that
runs your program
- …
- //GO.INPUT1 DD DSN=MY.INPUT,DISP=SHR
|
|
20
|
- For the programs in this course, you will use the ENVIRONMENT DIVISION’s
SELECT/ASSIGN clauses to point to ASCII files on your machine.
- So your “environment” is the PC, not the mainframe J
- You will code your Select/Assign clauses for PC files
like the following:
- ENVIRONMENT DIVISION.
- INPUT-OUTPUT SECTION.
- FILE-CONTROL.
- SELECT CUST-FILE
- ASSIGN TO "D:\CUST.DAT"
- ORGANIZATION IS LINE SEQUENTIAL
- ACCESS MODE IS SEQUENTIAL
- FILE STATUS IS CFCODE.
- SELECT PRINT-FILE
- ASSIGN TO "D:\CUSTOUT.DAT"
- ORGANIZATION IS LINE SEQUENTIAL
- ACCESS MODE IS SEQUENTIAL
- FILE STATUS IS PRTCODE.
- See Slide Notes, for additional File Read/Write performance tips
|
|
21
|
|
|
22
|
- After completing this topic, you should be able to:
- Describe the purpose of the DATA DIVISION
- List the required elements of it
- Code a syntactically correct DATA DIVISION
- List a few optional paragraphs and sections of the DATA DIVISION
|
|
23
|
- DATA DIVISION.
- FILE SECTION.
- …
- WORKING-STORAGE SECTION.
- …
- LINKAGE SECTION.
- One of COBOL’s fortes is file handling and data manipulation – which
COBOL makes:
- Simple – easy to code
- Efficient – as pertains to run-time processing efficiency)
- Sophisticated – allowing for complex data types and structures
- Define the characteristics of your data, and group your data definitions
into one of the sections in the DATA DIVISION.
- You can use these sections of the DATA DIVISION to define the following
types of data:
- Data used in input-output operations (FILE SECTION)
- Data defined for internal processing purposes, calculation fields,
flags, counters, etc. (WORKING-STORAGE SECTION)
- Data passed as parameters from another program (LINKAGE SECTION)
|
|
24
|
- DATA DIVISION.
- FILE SECTION.
- FD internalFileName
- RECORD CONTAINS n CHARACTERS
- DATA RECORD IS recordVariable.
- 01 recordVariable.
- 05 Field1 PIC X(40).
- 05 Field2 PIC X(40).
- Every Select/Assign file in the INPUT-OUTPUT SECTION of the ENVIRONMENT
DIVISION must have a matching FD (File Definition) entry in the FILE
SECTION
- You follow an FD by the declaration of the data record the file is read
into/written from
- The RECORD CONTAINS clause is optional – but if explicitly coded, it
causes the compiler to verify the DATA RECORD’s overall length with the
“n CHARACTERS” specified
- The DATA RECORD clause is also optional, and specifies the COBOL
variable that will be read into, or written from. .
- FILE SECTION, FD and the 01 level-number for the record variable are
coded in the “A” area
- All other clauses and level-numbers > 01 must begin in the “B” area
- $ Note that we'll cover
COBOL level-numbers in a few slides
|
|
25
|
- DATA DIVISION.
- FILE SECTION.
- FD internalFileName
- …
- BLOCK CONTAINS n RECORDS
- LABEL RECORDS ARE STANDARD.
- BLOCK CONTAINS – defines a “blocking factor” for the file.
- Blocking factors are I/O efficiency features of COBOL and z/OS
- We will cover them later in the course
- For mainframe COBOL programs:
- Recommendation - code: BLOCK CONTAINS 0 RECORDS
- This allows the compiler to obtain the Blocking Factor directly from
the program’s JCL
- For PC COBOL programs:
- Recommendation – leave this clause out
- LABEL RECORDS ARE STANDARD – determines whether there is a label on the
z/OS dataset (external file).
- -LABEL RECORDS ARE STANDARD is the default if you omit the clause
- -Leave this clause off for PC files, print files and non-labeled
datasets
|
|
26
|
- DATA DIVISION.
- FILE SECTION.
- …
- WORKING-STORAGE SECTION.
- RECORD-KTR PIC 9(4).
- 77 END-OF-FILE-SW PIC X(1) VALUE 'N'.
- 01 StructureVariable.
- 05 Elementary-num-field PIC
9(4).
- 05 Elementary-char-field PIC
X(40).
- The WORKING-STORAGE SECTION is where you code variables used throughout
your program
- All of the variables in the WORKING-STORAGE SECTION are global in scope (can be referenced
throughout your program)
- Use WORKING-STORAGE variables for:
- Temporary fields used in calculations
- Variable names for constant values
- Record structures used in data manipulation processing
- Switches and flags (i.e.
end-of-file)
- Print file records and structures
|
|
27
|
- WORKING-STORAGE SECTION.
- RECORD-KTR PIC 9(4) COMP VALUE ZERO.
- 77 KTR-2 PIC 9999 COMP VALUE
ZERO.
- 01 STRUCTURE-VARIABLE.
- 05 ELEM-FIELD PIC 9(7)V99
COMP-3 VALUE ZERO.
- 05 END-OF PIC X(40).
- COBOL variable names:
- Can use alphanumerics and the dash (but don’t start or end a variable
name with a dash)
- Must be shorter than 30 characters, no embedded blanks L, and contain at least one
letter
- Code individual (single) COBOL variables as follows:
- Type a 77 in the “A” area
- Type the variable name in the “B” area
- Follow the variable name with a “picture clause” – typically
abbreviated as: PIC – that
describes the type, size and scale of your variable
- End with the variable declaration with a period
- Code (related) variables grouped into a structure as follows:
- Type a 01 in the “A” area
- Type the structure name in the “B” area
- Type the remaining variables starting with a number > 01 in the “B”
area and follow the above rules for coding 77 variables
- ! See Slide Notes – for an
explanation of COMP and COMP-3
|
|
28
|
- Every COBOL data variable must have a picture clause – typically
abbreviated as: PIC
- A variable’s picture clause determines:
- The type and format of the data stored in the field
- Size of the data
- Numeric precision (scale, or # of digits to the right and left of the
decimal place)
- You can code a number inside a parenthesis, which means that the
preceding character is repeated “n” times (see below)
- Different PIC clauses are used for input and intermediate calculation
results or holding areas and for output.
- ! Note: We’ll cover input
and output picture clauses in-depth, in a subsequent unit of this
course.
- The Input or WORKING-STORAGE PIC clauses consist of:
- X
- Any character (Alpha, numeric, or special)
- 9
- Only numeric data - digits (0 à9)
- V
- Part of numeric definition.
Assumed decimal point
- S
- Part of numeric definition. Sign – allows for negative numbers
|
|
29
|
- 01 ADDRESS-REC.
- 05 STREET PIC X(40).
- 05 ADDRESS-2.
- 10 CITY PIC X(17).
- 10 STATE-CODE PIC XX.
- 10 POSTAL-CODE1 PIC 9(5).
- 10 FILLER PIC X VALUE "-".
- 10 POSTAL-CODE2 PIC 9(4).
- You may want to sub-divide data into groups of fields that show
relationships, or that organize
variables in a hierarchical structure known as a COBOL “record”.
- Records are COBOL variables that
begin with a “level-number” in the range of 01 (which denotes the record as a
whole) è 49.
- COBOL records consist of:
- Group fields:
- Which do NOT have a picture clause
- Typically contain elementary fields (lower level fields that
sub-divide a group variable)
- And have a lower level number (representing higher “parentage” level”
in the structure) than their children (elementary) fields
- Elementary fields:
- Which must have a picture clause
- And have a higher level number (representing lower or dependent level
in the structure) than their group field “parent” variables
- You can manipulate both group and elementary fields in COBOL
|
|
30
|
- DATA DIVISION.
- FILE SECTION.
- WORKING-STORAGE SECTION.
- LINKAGE SECTION.
- 01 StructureVariableName.
- 05 BinaryIntVariable PIC 9(4)
COMP.
- 05 FixedStringVariable PIC
X(40).
- The LINKAGE SECTION is where you define variables that are used to
receive parameters passed by other programs in your application
- All of the variables in the LINKAGE SECTION are global in scope
- LINKAGE SECTION variables adhere to the same COBOL variable coding
rules as data in the FILE SECTION and WORKING-STORAGE SECTION:
- COBOL variable naming rules
- Picture clause definitions
- Group/Structure record declaration rules
- LINKAGE SECTION variables obtain their values through data passed as
parameters in “calling” COBOL programs
- This will be described in greater detail later in the course
|
|
31
|
- Memorizing all these rules seems like a steep ladder to climb…
|
|
32
|
|
|
33
|
- After completing this topic, you should be able to:
- Describe the purpose of the PROCEDURE DIVISION
- List the required elements of it
- Code a syntactically correct PROCEDURE DIVISION statement
- List a few optional paragraphs and sections of the PROCEDURE DIVISION
|
|
34
|
- IDENTIFICATION DIVISION.
- …
- ENVIRONMENT DIVISION.
- …
- DATA DIVISION.
- …
- PROCEDURE DIVISION.
- In the PROCEDURE DIVISION of a program, you code the executable
statements (business logic) that process data.
- The division header for a program can be:
- PROCEDURE DIVISION.
- PROCEDURE DIVISION USING VAR1, VAR2…
- PROCEDURE DIVISION RETURNING VAR1, VAR2…
- -- Note that VAR1, VAR2, etc. are variables defined in a program’s LINKAGE SECTION
|
|
35
|
- Section
- Logical subdivision of your processing logic.
- A section has a procedure name – declared in the “A” area, and is
optionally followed by one or more paragraphs.
- A section can be the subject of a PERFORM statement.
- A section can have zero-to-many paragraphs
- Paragraph
- Subdivision of a section, procedure, or program.
- A paragraph has a procedure name – declared in the “A” area, followed by
a period and zero or more sentences.
- A paragraph can be the subject of a PERFORM or THRU statement.
- Sentence
- Series of one or more COBOL statements
– coded in the “B” area, that ends with a period.
- Statement
- Performs a defined step of COBOL processing, such as adding two numbers.
- A statement is a valid combination of words, and begins with a COBOL
verb.
- Statements are
- Imperative (indicating unconditional action)
- Conditional
- Compiler-directing
- Statements are terminated by a period
- Some statements can be terminated by an explicit Scope Terminator (see
screen capture)
- Using explicit scope terminators instead of periods to show the logical
end of a statement is a “Best Practice”
|
|
36
|
- An imperative statement - such as
- ADD
- MOVE
- INSPECT
- COMPUTE
- DISPLAY
- ACCEPT
- GOBACK
- READ
- WRITE
- CLOSE
- …indicates an unconditional
action to be taken.
- End an imperative statement with a period
- Or if allowed (READ, WRITE, COMPUTE), end with a scope-terminator
|
|
37
|
- A conditional statement is either a simple conditional statement
- or a conditional statement made
up of an imperative statement that includes a conditional phrase or
option
- PERFORM … UNTIL
- READ … AT END
- You can end a conditional statement with an implicit or explicit scope
terminator.
|
|
38
|
- WORKING-STORAGE SECTION.
- 77 INPUT-DATA PIC X(40).
- …
- PROCEDURE DIVISION.
- …
- ACCEPT INPUT-DATA.
- Accept waits for user data entry from your keyboard (and waits for you
to press 8 Enter.
- The alpha-numeric value you enter is assigned to the variable following ACCEPT.
- It is not necessary to make the accepted input variable of type PIC X –
but is probably a best practice, to avoid user-entry and data-type
errors
- On the Mainframe, ACCEPT retrieves its value from a reserved JCL “DD
card” named: SYSIN.
- We will discuss this in more depth later in the course
|
|
39
|
- ACCEPT is often used to retrieve certain useful operating system
"special-register" values:
- Current Date – formatted with a number of different date masks:
- Date
- DAY-OF-WEEK (a value 1 à
7, representing Monday à Sunday)
- Day – the ordinal day of the year (1 à 365)
- Time – formatted from the operating system's time: hhmmssss
|
|
40
|
- WORKING-STORAGE SECTION.
- …
- PROCEDURE DIVISION.
- …
- DISPLAY "Something on the
console".
- Display presents a literal or the contents of a PIC X(nn) field in the
console:
- On the mainframe DISPLAY writes to a reserved JCL DD statement named: //SYSOUT
- On your PC a DOS window is opened, and the data is displayed inside it
- Also – a new line is opened and your cursor moves to the new line
(which works, in case you want to ACCEPT data into the program)
- You can DISPLAY:
- A literal
- A PIC X field
- A PIC 9 field
- A record
- Multiple fields and/or literals, separated by commas
- Code DISPLAY in the "B" area of your program
|
|
41
|
- FILE SECTION
- …
- 01 OUT-REC PIC X(80).
- …
- WORKING-STORAGE SECTION.
- 77 INPUT-DATA PIC X(40).
- …
- PROCEDURE DIVISION.
- …
- ACCEPT INPUT-DATA.
- MOVE INPUT-DATA TO OUT-REC.
- Syntax: MOVE <to-variable> TO
<from-variable>.
- MOVE copies the value in the to-variable to the from-variable (receiving
field), over-writing the from-variable’s storage contents.
- Alpha-numeric values are copied from left-to-right, byte-for-byte
- If the receiving field is longer than the sending field blanks (spaces)
pad the receiving field to the end of its PIC X length
- If the receiving field is shorter than the sending field truncation
occurs
- We will study MOVE variations, and COBOL internal storage contents in
subsequent chapters
|
|
42
|
- FILE SECTION
- FD EXTFILE …
- 01 OUT-REC PIC X(80).
- …
- WORKING-STORAGE SECTION.
- …
- PROCEDURE DIVISION.
- …
- OPEN <input/output> EXTFILE.
- …
- WRITE OUT-REC.
- …
- CLOSE EXTFILE.
- COBOL file I/O consists three steps (done roughly in this order):
- OPEN the file you wish to read from or write to – for INPUT or OUTPUT (see
example next slide)
- Begin an iterative processing loop.
Somewhere in the loop:
- READ the input file - until “end-of-file” …and/or…
- WRITE the record in the COBOL FILE SECTION FD – for all records that
should be written out
- CLOSE the file
- In subsequent chapters we will cover COBOL loop statements
|
|
43
|
- ENVIRONMENT DIVISION.
- INPUT-OUTPUT SECTION.
- SELECT INTFILENAME ASSIGN TO …
- DATA DIVISION.
- FILE SECTION
- FD INTFILENAME…
- 01 OUT-REC.
- …
- PROCEDURE DIVISION.
- OPEN OUTPUT INTFILENAME.
- WRITE OUT-REC.
- CLOSE INTFILENAME.
- Syntax: OPEN OUTPUT INPUT
EXTEND <InternalFileName>.
- OPEN connects your program to the external device assigned in your ENVIRONMENT
DIVISION.
- Your program must issue an OPEN statement, before you can READ to or
WRITE from the file
- OPEN INPUT – for files you READ from
- OPEN OUTPUT – for files you WRITE to from scratch (over-writing the
contents of any existing records in the file)
- OPEN EXTEND – for files you WRITE to and append new records starting
at the end of the file
- CLOSE all OPEN files before ending your program
- You can OPEN multiple files in one statement (see Slide Notes)
|
|
44
|
- ENVIRONMENT DIVISION.
- INPUT-OUTPUT SECTION.
- SELECT INTFILENAME ASSIGN TO …
- DATA DIVISION.
- FILE SECTION
- FD INTFILENAME…
- 01 OUT-REC.
- …
- PROCEDURE DIVISION.
- OPEN OUTPUT INTFILENAME.
- WRITE OUT-REC.
- CLOSE INTFILENAME.
- Syntax: WRITE
<OUTPUT-RECORD>.
- WRITE adds one record to the end of an OPEN OUTPUT file.
- You write:
- The record in the FD for the file in the FILE SECTION
- The record in the FD for the file in the FILE SECTION FROM a record in
the WORKING-STORAGE SECTION
|
|
45
|
- ENVIRONMENT DIVISION.
- INPUT-OUTPUT SECTION.
- SELECT INTFILENAME ASSIGN TO …
- DATA DIVISION.
- FILE SECTION
- FD INTFILENAME…
- 01 OUT-REC.
- …
- PROCEDURE DIVISION.
- OPEN OUTPUT INTFILENAME.
- WRITE OUT-REC.
- CLOSE INTFILENAME.
- Syntax: CLOSE
<InternalFileName>.
- Issue a CLOSE statement for every file you are using before your program
ends. CLOSE:
- Closes an external file
- And makes it unavailable for processing
- Unless/until a subsequent OPEN statement occurs
- You can close multiple files in a single statement
|
|
46
|
|
|
47
|
|
|
48
|
|