Thursday, September 3, 2009

THE SEQUENTIAL FILE ORGANIZATION

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.