|
File Processing
File Processing
All file processing operations are held in procedure division.
File Operations: OPEN
READ
WRITE
REWRITE
CLOSE
OPEN EMPFILE:
OPEN {INPUT, OUTPUT, EXTEND, I-O} file-name-1 [, file-name-2] . . .
The OPEN statement initiates the processing of files. The success-
ful execution of an OPEN statement determines the availability of
the file for processing. The successful execution of the OPEN
statement makes the associated record area available to the program;
it does not obtain or release the first data record. If the FILE
STATUS clause is specified in the FILE-CONTROL entry, the associa-
ted operation status is updated when the OPEN statement is executed.
A sequential file can be opened in one of the following four modes.
INPUT, OUTPUT, EXTEND and I-O.
A file can be opened in the INPUT mode only if it already exists.
Such a file becomes an input file from which records can be read
sequentially.
When a file is to be created for the first time, it must be opened
in the OUTPUT mode. File can be written in this mode.
The EXTEND mode also opens a file for writing, but the file pointer
is positioned after the end of the last record. Thus any records
written will get appended to the file.
A file is opened in the I-O mode when it needs to be updated. This
mode provides both reading and rewriting of records.
CLOSE EMPFILE:
This statement terminates processing of file. In COBOL-85 this
CLOSE Statement is optional and STOP RUN automatically closes the
File if it is not explicitly closed. This CLOSE statement means
termination of link between Physical file and logical file.
READ EMPFILE:
If a file is opened in INPUT or I-O mode then a READ statement
make available Next logical record for processing. The primary
function of the READ statement is to fetch records from a file
and place the file pointer at an appropriate position after READ;
it performs certain checks to ensure proper execution of the
program.
READ EMPFILE
AT END imperative statement.
END-READ
If the file reached end of the file and if program tries to read
a record then AT END condition satisfy and imperative statement
will performed.
If INTO condition specified like READ EMPFILE INTO WS-RECORD.
Here WS-RECORD is data division data name. Program read the file
and places record into WS-RECORD.
WRITE statement:
WRITE CAPGEMINI-REC FROM WS-RECORD
END-WRITE.
File opened in OUTPUT or EXTEND mode then we can use WRITE state-
ment to write records into the file. If from is used then the data
in WS-RECORD is moved to record and writing take place. If FROM
is omitted then Data moved to record name will be written into
file. If we are reading file we refer file name in READ statement,
where as while writing we refer record name with WRITE command.
REWRITE statement:
REWRITE command is used to update a record in a file. If file is
opened in I-O mode then only we can use REWRITE command on that
file. This REWRITE is not available in Sequential file. Before
using REWRITE command, corresponding record should be read.
A Sample COBOL Program that READ a Sequential files and Write
into another file.
Click here to see sample cobol program
|
|