In this file organization, the records of the file are stored one after another both physically and logically. That is, record with sequence number 16 is located just after the 15th record.
A record of a sequential file can only be accessed
by reading all the previous records.
The records are discriminated from one another using
the record length declared in the associated FD statement of the
FILE-SECTION. For example, If the record structure that the programmer
has declared is 52 bytes, blocks of 52 byte data (records) are
assumed to placed one after another in the file. If the programmer
is reading the data in a sequential file, every READ statement
brings 52 bytes into the memory.
If the file contains, say, 52 byte records; but the
programmer tries to read this file with a program which has declared
40 byte records (i.e the total length of the FD structure is 40
bytes), the program will certainly read some pieces of information
into the memory but the after the first READ statement, some meaningless
pieces of records will be brought into memory and the program
will start processing some physical records which contain logically
meaningless data.
It is the programmer's responsibility to take care
of the record sizes in files. You must be careful when declaring
record structures for files. Any mistake you make in record sizes
will cause your program to read/write erroneous information. This
is especially dangerous if the file contents are being altered
(changed, updated).
Since the records are simply appended to each other
when building SEQUENTIAL files, you simply end up with a STREAM
of byte. If this string does not contain any "Carriage Return/Line
Feed" control characters in it, the whole file will appear
as a single LINE of character and would be impsossible to process
with regular text editors. As you should know by now, text editors
are good in reading/writing/modifying text files. These programs
will assume that the file consists of LINES and expect the lines
to separated from each other by a pair of control characters called
"Carriage Return/Line Feed" (or CR/LF).
COBOL has a special type of sequential file organization,
which is called the LINE SEQUENTIAL ORGANIZATION which places
a CR/LF pair at the end of each record while adding records to
a file and expect such a pair while reading. LINE SEQUENTIAL files
are much easier to use while developing programs because you can
always use a simple text editor to see the contents of your sequential
file and trace/debug your program.
Please note that LINE SEQUENTIAL files have two extra
characters for each record. For files, which have millions of
records, this might use up a significant amount of disk space.
SEQUENTIAL files have only one ACCESS MODE and that
is "sequential access". Therefore you need not specify
an ACCESS MODE in the SELECT statement. Typical SELECT statements
for SEQUENTIAL files are :
SELECT MYFILE ASSIGN TO DISK "MYFILE.DAT" ORGANIZATION IS SEQUENTIAL. SELECT MYFILE-2 ASSIGN TO DISK "C:\DATADIR\MYFILE2.TXT" ORGANIZATION IS LINE SEQUENTIAL.
In the FILE-SECTION, you must provide FD blocks for
each file; hence for a sequential file you could have something
like :
FD MYFILE. 01 MYFILE-REC. 02 M-NAME PIC X(16). 02 M-SURNAME PIC X(16). 02 M-BIRTHDATE. 03 M-BD-YEAR PIC 9999. 03 M-BD-MONTH PIC 99. 03 M-BD-DAY PIC 99.
Note : You must
NOT provide record fields for the extra two CR/LF bytes in record
descriptions of LINE SEQ files. Once you declare the file to be
a LINE SEQ file, these two extra bytes are automatically taken
in consideration and added for all new records that are added
to a file.
Like all files, sequential and line sequential files
must be OPENed before they can be processes and CLOSEd
when they are not needed anymore.
Seq and Line Seq files can be opened for :
Opening a seq file for OUTPUT means that the
program will only issue WRITE statements to a NON-EXISTING and
JUST CREATED file. Therefore, when you open a file for OUTPUT,
COBOL assumes that the file does not exist and try to create
a new one. IF THE FILE EXISTS, ITS CONTENTS WILL BE CLEARED WHEN
OPENED AND ASSUMED TO BE A BRAND NEW FILE INTO WHICH RECORDS
WILL BE ADDED. You should be very careful when opening a file
for OUTPUT. One small mistake and all your valuable records
are lost forever.
Opening a seq file for INPUT means that the
program will only issue READ statements to an EXISTING file. Therefore,
when you open a file for INPUT, COBOL assumes that the file
exists and try to access it. IF THE FILE DOES NOT EXIST, AN
ERROR MESSAGE WILL BE ISSUED INDICATING THAT THE MENTIONED FILE
COULD NOT BE FOUND.
Opening a seq file for EXTEND means that the
program will add NEW RECORDS to an EXISTING file. Therefore, when
you open a file for EXTEND, COBOL assumes that the file exists
and subsequent WRITE statements will try to ADD NEW RECORDS at
the end of the existing file (in other words; the append mode).
Records can only be added to the END of a sequential file.
When you are finished with a file you must CLOSE
it.
You can close more than one files with a single CLOSE
statement. When a COBOL program terminates, all files mentioned
in the program are automatically closed; therefore you will not
get an error message for those files you forget to close or do
not close at all. Please note that relying on COBOL to close your
files is not a proper programming style. Although not absolutely
necessary, you should close your files when you are done with
them.
The CLOSE statement has a very important function.
Sometimes, your program needs to re-start reading a seq file from
the beginning. (Please note that every READ statement you execute,
brings the next record into memory and when you skip a record,
you cannot backspace the file to previous records.) In
such a situation, you will have to REWIND the file, that is make
the first record the active one in memory. You can achieve this
only by closing the file and re-opening it for INPUT.
You should be careful not to open an already opened
file. Closing already closed files usually do not cause any problems
but should be avoided. You should make sure that your program
opens and closes files at proper places in your logic flow so
that no input-output is tried on closed files.
Once you open a seq file, you can use READ or WRITE
statements to read or append records to this file. You cannot
use a READ statement for a file opened for OUTPUT or EXTEND and
similarly you cannot use a WRITE statement for a file you have
opened for INPUT.
A typical READ statement looks like :
A good programmer must check whether the READ statement
could find a record to read successfully. That means; the programmer
must check whether there were any records to READ when the statement
was executed. The situation can be checked with the following
construct :
The construct tells the COBOL compiler to execute
the paragraph labeled NO-RECORDS-FOUND when the READ statement
cannot find any records to read.
Another example could be :
Once your program EXECUTES a successful READ statement,
the information in the data record that was just brought into
memory will be available in the corresponding variables mentioned
in your record description declaration (the FD block).
When you want to put records in a seq file, you have
to open it for OUTPUT or EXTEND (depending on the requirements
of the algorithm of your program). The COBOL statement that is
used to put records into a file is the WRITE statement. The important
pointa that you should be careful with are that,
This will be best described by an example :
Suppose you want to add a new record to the file
You must first open it, then move new values to the
fields in MYFILE-REC and finally WRITE the record into the file.
Sometimes you need to make some small changes in
the contents of a file. Of course it possible to create a brand
new file with the new, modified contents but this is not practical.
COBOL provides you with an OPEN I-O mode with which you can modify
only the required record in a file.
In other words, there is another file opening mode;
the I-O mode; and another special REWRITE statement. Suppose that
you want to change the surname field of the 20th record
of a seq file (originally "AYFER") into "AYFEROGLU".
The program that you can write could read like
It is NOT possible to delete records of a seq
file. If you do not want a specific record
to be kept in a seq file any more, all you can do is to modify
the contents of the record so that it contains some special values
that your program will recognize as deleted (remember to
open the file in I-O mode and REWRITE a new record).
Can be only processed sequentially. If you need to
read record number N, you must first read the previous N-1 records.
Especially no good for programs that make frequent searches in
the file.
Some examples are :
OPEN OUTPUT NEW-FILE.
OPEN INPUT OLD-FILE.
OPEN EXTEND OLD-FILE OLD-FILE2.
OPEN INPUT OLD-FILE OUTPUT NEW-FILE.
CLOSE NEW-FILE.
CLOSE OLD-FILE NEW-FILE.
Simple INPUT-OUTPUT Statements for SEQ Files.
READ MY-FILE.
READ MY-FILE AT END PERFORM NO-RECORDS-FOUND.
READ MY-FILE AT END DISPLAY "No more records!"
CLOSE MY-FILE
STOP RUN.
SELECT MYFILE ASSIGN TO DISK "MYFILE.DAT"
ORGANIZATION IS SEQUENTIAL.
....
FD MYFILE.
01 MYFILE-REC.
02 M-NAME PIC X(16).
02 M-SURNAME PIC X(16).
02 M-BIRTHDATE.
03 M-BD-YEAR PIC 9999.
03 M-BD-MONTH PIC 99.
03 M-BD-DAY PIC 99.
OPEN EXTEND MYFILE.
....
MOVE "UGUR" TO M-NAME.
MOVE "AYFER" TO M-SURNAME.
MOVE 1955 TO M-BD-YEAR.
MOVE 1 TO M-BD-MONTH.
MOVE 1 TO M-BD-DAY.
WRITE MYFILE-REC.
.....
UPDATING RECORDS OF A SEQUENTIAL FILE
SELECT MYFILE ASSIGN TO DISK "MYFILE.DAT"
ORGANIZATION IS SEQUENTIAL.
....
FD MYFILE.
01 MYFILE-REC.
02 M-NAME PIC X(16).
02 M-SURNAME PIC X(16).
02 M-BIRTHDATE.
03 M-BD-YEAR PIC 9999.
03 M-BD-MONTH PIC 99.
03 M-BD-DAY PIC 99.
PROCEDURE DIVISION.
....
OPEN I-O MYFILE.
....
MOVE 20 TO N.
....
PERFORM READ-A-REC N-1 TIMES.
*
* TO MAKE SURE THAT WE ARE ON THE CORRECT RECORD
*
IF M-SURNAME NOT = "AYFER" DISPLAY "WRONG RECORD"
DISPLAY "RECORD NOT UPDATED"
CLOSE MYFILE
STOP RUN.
MOVE "AYFEROGLU" TO M-SURNAME.
REWRITE MYFILE-REC.
.....
CLOSE MYFILE.
....
DELETING RECORDS OF A SEQUENTIAL FILE
ADVANTAGES of SEQ FILES
DISADVANTAGE of SEQUENTIAL FILES