sams_teach_yourself_cobol_in_24_hours_-_hour_13_sequential_files

Sams Teach Yourself COBOL in 24 Hours - Hour 13 Sequential Files

Return to Teach Yourself COBOL in 24 Hours, COBOL bibliography, COBOL, COBOL DevOps, Awesome COBOL, Awesome IBM Mainframe, IBM Mainframe development, IBM Mainframe bibliography, Fortran

“ (TYCb24H 1998)

Part II

File Handling Hour

13 Sequential Files

14 Indexed Files

15 Reading Indexed File Records

16 Updating Indexed File Records

17 Sorting

Hour 13 Sequential Files

One of the many things that makes COBOL such a rich and powerful language is its ability to clearly, accurately, and quickly handle data files. Business deals with data files constantly. In this hour, you learn about one type of commonly used file: the sequential file. You learn

• How to define a sequential file in a COBOL program

• How to Open and Close the file

• How to create records in the file

• How to read records from a sequential file

• How to update existing records in a sequential file

In addition, COBOL uses some terms that you need to understand in order to discuss data file access.

Image

First, a file is made up of individual records. A record is a collection of individual fields or data items. The format or formats of the records in the file are defined in your COBOL program. A record is a Group Level item, made up of elementary data items or groups of elementary data items. The definition for a record is called a record layout, or record description. The layouts of the records used by the various files in your COBOL program are specified in the Data Division.

A file is simply a group of records. A sequential file is one that is accessed sequentially; that is, the records are retrieved from the file in order, from the first record in the file to the last. Records cannot be retrieved out of order. You may not jump ahead in the file, nor may you go backward.

When creating, or writing, a sequential file, you must write the records in order. The physical order of the records in the file is the order in which they were written.

Most PC-based compilers differentiate between two types of sequential files. The first is the default type, Record Sequential, and the other is Line Sequential. Line Sequential files are regular text files, created by Notepad or some other text editor. Your CONFIG.SYS and AUTOEXEC.BAT files are examples of Line Sequential files.

Image

Line Sequential files contain records of varying length. Trailing space data in the record is not written (that is, it is truncated), thus saving space. Records are terminated with a platform-dependent delimiter. Under most PC-based operating systems, this delimiter is a Carriage Return and Line Feed, in ACSII a X”0D0A”.

Image

On UNIX systems, Line Sequential files are terminated with a Line Feed only; in ASCII, a X”0A”.

The hexadecimal, or internal representation, of these characters is X”0D”, X”0A”. Only textual data may be written to these files. Many implementations ignore characters less than spaces when they are written to these files. For example, Low-Values do not appear in your Line Sequential data file if you attempt to use them in the record.

Line Sequential files are good for reading or writing data that the user may edit with a standard text editor. If your files are for use only by your COBOL program and are not to be shared with other systems or if your files contain packed data such as usage COMP or COMP-3 data items, you should not use Line Sequential files.

Image

The other type of file is a Record Sequential file, normally referred to simply as a Sequential file. The records in a Record Sequential file are not delimited. Each record in the file is adjacent to its immediate neighbors. Table 13.1 illustrates the difference between Line Sequential and Record Sequential files.

Table 13.1 Line Sequential Versus Record Sequential Organization

Image

The CRLF under the Line Sequential column demonstrates the delimiters in each record of a Line Sequential file. The delimiter immediately follows the last character in the record that is greater than a space. Under the Record Sequential column, imagine that the * characters are actually spaces. With a Record Sequential file, the trailing spaces are written to the file, and there is no delimiter. The next record in the file starts immediately after the current record.

Sequential files have many uses. For example, they can hold the data necessary to load a table for use in the COBOL program. Such files often hold transactional data to be used in an update of a master file. You can also use Sequential files to exchange data between systems and computers. Printers are written to as if they were Sequential files.

A Sequential file might be a disk file or a tape file. It could even be a paper tape or punch card file. Any device that can be attached to the computer and read from or written to in a sequential fashion can contain a Sequential file. On the PC, most Sequential files are located on disk.

Connecting Your Program to a File To access a Sequential file, you must establish a connection between the file and your program. This process requires two steps. The first establishes the hardware, file type, organization, and filename of the file you are accessing. The second defines the layout of the records in the file.

The Select Statement The Select statement connects your program to a file. Several clauses can be coded with the Select statement. This section considers only the clauses that relate to Sequential files.

The Select statement is coded in the Environment Division, under the Input-Output Section, in a paragraph titled File-Control.

An internal filename is specified in the Select statement. This name is the name by which you refer to the file in your COBOL program. The filename may be up to 30 characters long.

The Assign clause associates the file with a named file on your system. The Assign clause can refer to a symbolic name or, in some cases, as on the PC, an actual physical filename. The symbolic name can later be associated with a specific file, using runtime options. In the case of IBM mainframes, these files are associated using Job Control Language (JCL). In this book, the actual physical filenames to be used are defined. The Organization clause specifies the type of file you are working with. For Sequential files, the type can be Sequential or Line Sequential.

The File Status clause associates the system returned File Status with a field in the Data Division. This field is two characters long and contains a status value that can be tested after every operation against the file.

The following example is the Select statement for a file called Name-File in your COBOL program. The organization is Line Sequential. The actual filename on the PC is NAME.TXT. The File Status is stored in a field named Name-File-Status. The Select clause starts in Area B (column 12).

000010 @OPTIONS MAIN,TEST 000020 Identification Division. 000030 Program-Id. Chapt13a. 000031* File Creation Example 000043 Environment Division. 000050 Configuration Section. 000051 Source-Computer. IBM-PC. 000055 Object-Computer. IBM-PC. 000056 Input-Output Section. 000059 File-Control. 000060 Select Name-File Assign To “NAME.TXT” 000061 Organization Is Line Sequential 000062 File Status Is Name-File-Status. This program contains several new elements. Notice the new Section: Input-Output. It contains the paragraphs pertaining to external file I-O. The File-Control Paragraph is the heading under which your Select statements are coded.

Image

I-O is shorthand for “Input-Output.” Input is information that comes into your program. It might be user input, such as the data that is entered into a data entry program, or it might be a data file. Output is any information that your program produces. It can take many forms. Output might be data displayed on the screen or a data file, among other things.

The File Description The File Description (FD) describes the attributes of the file and its associated data record or records.

With modern COBOL, the only relevant line is the actual FD line. The FD is coded in the Data Division of the File Section. The Record Description, or descriptions, immediately follow the FD. The FD contains the same filename as specified in the Select statement. Every file specified with a Select statement requires a File Description entry.

The record description must start with an 01 Group Level item. A file may have more than one record description, and each must follow the associated FD. Your file has only one field: Full-Name. The FD is coded as follows:

000065 Data Division. 000066 File Section. 000067 FD Name-File. 000068 01 Name-Record. 000069 03 Full-Name Pic X(30). Image

The 01 level for this record can also be coded as 01 Name-Record Pic X(30) or as 01 Full-Name Pic X(30). However, as you will see when you start using this FD, this method is unclear in the program. A better approach is to name your record descriptions “record” and to name the individual fields that make up the record with names that are appropriate for their contents.

The rules for coding data items in the Record Description are nearly identical to those for dealing with Working-Storage. The data records can contain 88 level items and Redefines clauses. However, a few restrictions do apply. Any Value clauses specified are considered comments and do not set actual values within the fields. In addition, 77 level items may not be used in a Record Description. Occurs clauses may be used, even those that describe variable-length tables. However, the result is a variable-length record, and certain special rules must be followed. These records are discussed later in the hour.

Image

You should be aware that if you specify more than one Record Description for a file, these Record Descriptions are implicit Redefines. You can think of them as overlapping each other. Therefore, if you move data into a field in one Record Description, that data shows in all Record Descriptions for the file.

Opening the File Now that the file is defined to the COBOL program, you may use it. The first step is to Open the file. Sequential files may be opened in four different modes.

Opening the file for Input allows you to read data from the file. If you open the file for Output, you can write data to the file. Opening the file I-O allows you to update records in the file, and opening the file Extend allows you to add records to the end of the file.

To open a file for Input, code as follows:

000090 Open Input Name-File The Open will be successful if the file exists. The status value of a successful open is 00. This value is stored in the data item assigned by the File Status clause on the Select statement. If no File Status is defined, the program may end abnormally, with an error reported by the runtime system.

Standard File Status values contain two characters. The status returned under “normal” circumstances begins with a zero. If your file does not exist and you Open it Input, the File Status returned is 35.

If you don’t want your program to report a serious file error when the file does not exist, you can code the Optional clause on the Select statement. When Optional is coded, the File Status reported for the Open of a file that does not exist is 05 and the Open is successful. The first Read of the file, however, reports that end of file has been reached. The Optional clause is coded in the Select statement as follows:

000058 Input-Output Section. 000059 File-Control. 000060 Select Optional Name-File Assign To “NAME.TXT” 000061 Organization Is Line Sequential 000062 File Status Is Name-File-Status. Image

File Status values are discussed as you learn the different file operations. For a complete list of File Status values that can be returned with the Fujitsu compiler, please see the Cobol 85 User’s Guide, which is included with the Fujitsu compiler on the CD. If you prefer to use the Adobe Acrobat reader for viewing this document, the PDF format file is in the \SOFTCOPY\PDF directory of the CD.

Closing the File When you are finished processing a file, you should release that file to the operating system so that other programs can use it. The Close statement syntax closely follows the Open statement. The filename being closed, as stated in the Select statement and FD, must be specified.

000100 Close Name-File Most programmers do not check the File Status after the Close statement. However, the File Status values shown in Table 13.2. can be returned from the Close of a Sequential file.

Table 13.2 Sequential File Status Values After A Close Statement

Image

Image

The last status, 9x, actually contains a 9 in the first position and a vendor-defined value in the second position. This value can be just about anything that the compiler vendor desires.

Writing to the File Before you can accomplish anything meaningful with a Sequential file, you need to create it. To create the file, it is opened for Output. When a file that does not exist is opened for Output, it is created. If it does exist, it is replaced by an empty file. This concept is very important. When you Open a file for Output, you should intend to create a new file. The statement required to Open the name file for Output is

000091 Open Output Name-File After the Open, the File Status is checked to confirm the Open. Any status other than 00 indicates an error with the Open.

Table 13.3 Sequential File Status Values For Open In Output Mode

Image

The only error you are likely to encounter when opening a Sequential file for Output relates to the name chosen for your file or the media on which you are trying to create the file. For example, if you have a read-only CD in your CD-ROM drive and you attempt to Open a file Output on that drive, the File Status returned with the Fujitsu compiler is 90.

Data records are created in the file by using the Write statement. The only required operand with the Write statement is a record identifier. The record identifier is one of the 01 Group Level items coded under the FD.

Image

Remember, when writing to a file, never specify the filename, but rather the record identifier. The reason is that multiple record descriptions may exist for a particular file. Specifying the record description causes the program to Write the record in the format desired.

Several File Status values can be returned after a Write statement, as shown in Table 13.4.

Table 13.4 File Status Values from the Write Statement

Image

File Status 30 is a kind of catchall. Errors that may occur during the Write that are not captured any other way may report a File Status 30. Status 34 is reported if you exceed the maximum allowable size for the file on your platform or if the media you are writing to fills up. Status 48 is typically encountered when you have failed to Open the file or when you have opened it Input and are attempting to Write to the file.

The short program in Listing 13.1 demonstrates how to Open a file for Output, Accept names, and Write them to the file until F1 is pressed, and then Close the file and exit.

Listing 13.1 File Creation Example

000001 @OPTIONS MAIN,TEST 000002 Identification Division. 000003 Program-Id. Chapt13a. 000004* File Creation Example 000005 Environment Division. 000006 Configuration Section. 000007 Special-Names. 000008 Crt Status Is Keyboard-Status. 000009 Source-Computer. IBM-PC. 000010 Object-Computer. IBM-PC. 000011 Input-Output Section. 000012 File-Control. 000013 Select Name-File Assign To “NAME.TXT” 000014 Organization Is Line Sequential 000015 File Status Is Name-File-Status. 000016 Data Division. 000017 File Section. 000018 FD Name-File. 000019 01 Name-Record. 000020 03 Full-Name Pic X(30). 000021 Working-Storage Section. 000022 01 Keyboard-Status. 000023 03 Accept-Status Pic 9. 000024 03 Function-key Pic X. 000025 88 F1-Pressed Value X“01”. 000026 03 System-Use Pic X. 000027 01 File-Error-Flag Pic X Value Space. 000028 88 File-Error Value “Y”. 000029 01 Name-File-Status Pic XX Value Spaces. 000030 88 Name-File-Success Value “00”. 000031 01 Error-Message Pic X(50) Value Spaces. 000032 Screen Section. 000033 01 Name-Entry Blank Screen. 000034 03 Line 01 Column 01 Value “ Enter Name: ”. 000035 03 Line 01 Column 14 Pic X(30) Using Full-Name. 000036 03 Line 05 Column 01 Pic X(50) From Error-Message. 000037 03 Line 20 Column 01 Value “Press F1 to Exit”. 000038 Procedure Division. 000039 Chapt13a-Start. 000040 Perform Open-File 000041 If Not File-Error 000042 Perform Process-Input Until F1-Pressed Or 000043 File-Error 000044 Perform Close-File 000045 End-If 000046 Stop Run 000047 . 000048 Open-File. 000049 Open Output Name-File 000050 If Not Name-File-Success 000051 Move Spaces To Error-Message 000052 String “Open Error ” Name-File-Status 000053 Delimited By Size 000054 Into Error-Message 000055 Perform Display-And-Accept-Error 000056 End-if 000057 . 000058 Process-Input. 000059 Move Spaces To Full-Name 000060 Display Name-Entry 000061 Accept Name-Entry 000062 Move Spaces To Error-Message 000063 If Not F1-Pressed 000064 Perform Write-Record 000065 End-If 000066 . 000067 Write-Record. 000068 Write Name-Record 000069 If Name-File-Success 000070 Move “Record Written” To Error-Message 000071 Else 000072 String “Write Error ” Name-File-Status 000073 Delimited By Size 000074 Into Error-Message 000075 Perform Display-And-Accept-Error 000076 End-if 000077 . 000078 Display-And-Accept-Error. 000079 Set File-Error To True 000080 Display Name-Entry 000081 Accept Name-Entry 000082 . 000083 Close-File. 000084 Close Name-File 000085 . As you read through this program, notice the File Status checks after the Open and the Write. If an error of any kind occurs, the error flag is set and the status and type of error are displayed. Records are written to the file until someone presses the F1 key or a file error occurs.

After a Write, the contents of the file buffer cannot be counted on. The file buffer is the area described by the record description under the FD. Therefore, if you need to reference the contents of the data record after a Write, you need to store the record in Working-Storage. Simply create a Record Description that is a single elementary item, long enough to hold the data record. Then manipulate and use the record as defined in Working-Storage. When you Write the record, you can do one of two things. You can either move the Working-Storage version of the record to the record description and then issue the Write, or you can use the Write statement with the From clause. Using From causes the program to do an implied move. The data in Working-Storage is moved to the file buffer as the Write is processed.

Instead of the FD coded previously, you may use the following:

000018 FD Name-File. 000019 01 Name-Record Pic X(30). Add the following line to Working-Storage.

000021 Working-Storage Section. 000022 01 Full-Name Pic X(30) Value Spaces. The only other change necessary is to the Write statement:

000068 Write Name-Record From Full-Name Now you can reference Full-Name with the Display statement, after the Write is complete, without worrying about the integrity of the data in the file buffer.

Enter, compile, link, and run this program. After entering several names and exiting the program, use Notepad or some other text editor to open and examine Name.Txt in the \TYCOBOL folder. You can see that each record appears on a separate line.

What happens if you want to add data to the end of the file? Every time you run the program and the file is opened for Output, the previous data is lost. You can change the Open statement to Open the file Extend, instead of Output. When the file is opened Extend, new data records written to the file are added at the end of the file, after the existing records.

000049 Open Extend Name-File When opening a file Extend, two new File Status values come into play. If the file does not exist, a File Status value of 35 is returned, which tells you that the file does not exist. If you want to always add to the end of a file, this condition makes it difficult. When running the program, you may not know whether the file exists or not! Changing the Select clause to include Optional cures this problem. If you Open an Optional file Extend and the file does not exist, the file is created and a File Status value of 05 is returned.

Change the filename that Name-File is assigned to in the sample program to “NAMES.TXT”. Change the Open statement from Output to Extend. Then compile, link, and run the program. Notice the File Status of 35 that is returned when the program runs.

Image

If you run the program with the Fujitsu compiler, a Non File message appears before the program actually seems to run. These Fujitsu messages are nice for diagnosing problems but should be turned off when your program is fully debugged. To turn off this feature, you need to change a runtime option. When you run the program and the runtime options window appears, in the Environment Variables Information field type in @NoMessage=YES. Click Set and then click Save. Follow the prompts. Then click OK to run the program. The error message window is now disabled.

Add the word Optional to the Select statement:

000013 Select Optional Name-File Assign To “NAME.TXT” Now compile and run the program. An 05 error is still reported. You need to change the 88 level conditional item for success to consider 00 and 05 as successful return codes.

000029 01 Name-File-Status Pic XX Value Spaces. 000030 88 Name-File-Success Value “00” “05”. When you run the revised program, new records are added to the end of Names.Txt. Try exiting and running the program multiple times, examining the file created. (You can use Notepad or another text editor.) Note that new records are added to the end of the file.

Reading from the File You may retrieve data from a sequential data file. Reading from a sequential data file requires that you Open the file for Input, or for I-O. Opening the file I-O is covered in following section, “Updating the File.” The statement required to Open the file for Input is

000110 Open Input Name-File When a file is Open for Input, you may only retrieve, or Read, data from the file. You may not update or write data to the file. A few new File Status values are reported when a file is opened for Input.

Table 13.5 Sequential File Status Values For Open In Input Mode

Image

One of the new File Status values is 05. If a file with Optional coded on the Select statement is opened for Input and that file does not exist, the Open is successful and a return code of 05 is returned. The file is not created. This feature is useful when you have a program that expects input data, but where you may not always have any input data to provide. By making the file Optional, the Open never fails, and the file is not created when it is opened. Additionally, you do not have to create an empty file to satisfy the program’s need for a file.

File Status 35 means that the file is not defined as Optional and does not exist.

File Status 39 means that the definition of the file being opened differs from that in the program. This condition usually does not affect Sequential files, but is possible.

File Status 41 means that you are attempting to Open a file that is already open.

You may retrieve data from an open file with the Read statement. Sequential files are read from the first record to the last. You may not skip forward in the file. Every record is read in order. Each Read returns the next record in the file.

When you code the Read statement, the filename is specified. You do not read using a record description. The simplest form of the Read statement is

000111 Read Name-File This Read statement returns the next record in the file and places the contents in the record description defined for the file under the FD. Several File Status values can be returned from a Read statement with a Sequential file (see Table 13.6).

Table 13.6 Sequential File Status Values For Read

Image

File Status 04 is considered a successful Read. However, the record read has a different size than your program’s definition.

File Status 10 means that you have reached the end of your input file and no record is returned. The previously read record was the last one in the file.

File Status 46 occurs when you attempt to Read a record and the previous Read has failed. This condition can occur if you happen to have reached end of file and then attempt to Read another record.

File Status 47 means that you have attempted to Read from a file that is either not Open or is Open Output or Extend, instead of Input or I-O.

The end-of-file condition can be detected in two ways. One method is to check the File Status after the Read. If it is 10, then you have reached the end of the file. Another way is to code the At End clause on the Read statement.

When At End is coded, the statements after the clause are executed when an end-of-file condition is detected. When using At End, I suggest you use the End-Read explicit terminator. In addition to coding At End, you may also code Not At End. This clause allows you to perform different statements depending on the status of end of file. If you are at the end of the file, you may want to do some special processing.

000120 Read Name-File 000121 At End Set All-Done To True 000122 Not At End Perform Process-Data 000123 End-read You may also store the results of a Read statement in a data item in Working-Storage. This is similar to the Write statement with From where the record is written from another data item. For the Read statement, you specify Into and the name of the data item in which you wish to store the record read.

000124 Read Name-File Into Full-Name Revise the program that wrote the Names.Txt file (see Listing 13.2). It should now Read a new record every time the user presses Enter and quit when the end of file is reached or the user presses F1.

Listing 13.2 Read Example

000001 @OPTIONS MAIN,TEST 000002 Identification Division. 000003 Program-Id. Chapt13d. 000004* Read Example 000005 Environment Division. 000006 Configuration Section. 000007 Special-Names. 000008 Crt Status Is Keyboard-Status. 000009 Source-Computer. IBM-PC. 000010 Object-Computer. IBM-PC. 000011 Input-Output Section. 000012 File-Control. 000013 Select Optional Name-File Assign To “NAMES.TXT” 000014 Organization Is Line Sequential 000015 File Status Is Name-File-Status. 000016 Data Division. 000017 File Section. 000018 FD Name-File. 000019 01 Name-Record Pic X(30). 000020 Working-Storage Section. 000021 01 Full-Name Pic X(30) Value Spaces. 000022 01 Keyboard-Status. 000023 03 Accept-Status Pic 9. 000024 03 Function-key Pic X. 000025 88 F1-Pressed Value X”01”. 000026 03 System-Use Pic X. 000027 01 File-Error-Flag Pic X Value Space. 000028 88 File-Error Value “Y”. 000029 01 Name-File-Status Pic XX Value Spaces. 000030 88 Name-File-Success Value “00” “05”. 000031 88 End-of-File Value “10”. 000032 01 Error-Message Pic X(50) Value Spaces. 000033 Screen Section. 000034 01 Name-Entry Blank Screen. 000035 03 Line 01 Column 01 Value “ Name: ”. 000036 03 Line 01 Column 14 Pic X(30) Using Full-Name. 000037 03 Line 05 Column 01 Pic X(50) From Error-Message. 000038 03 Line 20 Column 01 Value “Press F1 to Exit”. 000039 Procedure Division. 000040 Chapt13d-Start. 000041 Perform Open-File 000042 If Not File-Error 000043 Perform Process-File Until F1-Pressed Or 000044 File-Error Or 000045 End-Of-File 000046 Perform Close-File 000047 End-If 000048 Stop Run 000049 . 000050 Open-File. 000051 Open Input Name-File 000052 If Not Name-File-Success 000053 Move Spaces To Error-Message 000054 String “Open Error ” Name-File-Status 000055 Delimited By Size 000056 Into Error-Message 000057 Perform Display-And-Accept-Error 000058 End-If 000059 . 000060 Process-File. 000061 Move Spaces To Full-Name 000062 Perform Read-File 000063 If Not File-Error 000064 Display Name-Entry 000065 Accept Name-Entry 000066 End-If 000067 Move Spaces To Error-Message 000068 . 000069 Read-File. 000070 Read Name-File Into Full-Name 000071 At End Move “End Of File” To Error-Message 000072 End-Read 000073 If Name-File-Success Or End-Of-File 000074 Continue 000075 Else 000076 Move Spaces To Error-Message 000077 String “Read Error ” Name-File-Status 000078 Delimited by Size Into Error-Message 000079 End-String 000080 Perform Display-And-Accept-Error 000081 End-If 000082 . 000083 Display-And-Accept-Error. 000084 Set File-Error To True 000085 Display Name-Entry 000086 Accept Name-Entry 000087 . 000088 Close-File. 000089 Close Name-File 000090 . Make note of the use of the File Status; also note the At End condition test on the Read statement. The Read Into is the Full-Name field, which is used by the Screen Section.

Image

When you run the program, you will note that the Full-Name field is cleared when the At End condition is encountered. This condition may or may not occur with other compilers. Some compilers leave the value of the last successfully read record in the input buffer, or record description. Most will not.

Updating the File Image

In addition to reading and writing, you may also update the file. Some very special restrictions apply to updating a Sequential file. Because Line Sequential File records can be of differing lengths and updated records are written to the original physical location, you may not update a Line Sequential file. Take a moment now to change the program that writes the file (Chapt13a.Cob, Listing.13.1) Change the filename from Names.Txt to Names.Seq. Change the Select statement from Line Sequential to Sequential. Create some records with this program so that you can update them with the next example (Listing 13.3).

To update the file, you must Open it for I-O. The File Status values returned by the Open are the same as those reported for opening the file for Input.

000125 Open I-O Name-File After the file is Open, it is processed by Read statements as if it were Open for Input. However, you may now update a record by coding the Rewrite statement. Rewrite replaces the last record read with the new data that you have placed in the record description. Rewrite also supports the use of From to update the record from a data item in Working-Storage. The File Status values returned from a Rewrite on a Sequential file are the same as those that are returned as the result of a Write. The Rewrite statement also requires the record description and not the filename—exactly the same as the Write statement.

000125 Rewrite Name-Record From Full-Name Image

You may not issue a Write statement against a Sequential file that is opened I-O. If you need to Write more records to a Sequential file, you must open it Extend.

The program in Listing 13.3 updates a Sequential file. If the user presses Enter, the program reads the next record. Pressing F1 ends the program; pressing F2 updates the last record with the name entered by the user. Enter, compile, link, and run this program. Experiment with its operation to see how the Rewrite statement updates records.

Listing 13.3 Update Example

000001 @OPTIONS MAIN,TEST 000002 Identification Division. 000003 Program-Id. Chapt13f. 000004* Update Example 000005 Environment Division. 000006 Configuration Section. 000007 Special-Names. 000008 Crt Status Is Keyboard-Status. 000009 Source-Computer. IBM-PC. 000010 Object-Computer. IBM-PC. 000011 Input-Output Section. 000012 File-Control. 000013 Select Optional Name-File Assign To “NAMES.SEQ” 000014 Organization Is Sequential 000015 File Status Is Name-File-Status. 000016 Data Division. 000017 File Section. 000018 Fd Name-File. 000019 01 Name-Record Pic X(30). 000020 Working-Storage Section. 000021 01 Full-Name Pic X(30) Value Spaces. 000022 01 Keyboard-Status. 000023 03 Accept-Status Pic 9. 000024 03 Function-Key Pic X. 000025 88 F1-Pressed Value X”01”. 000026 88 F2-Pressed Value X”02”. 000027 03 System-Use Pic X. 000028 01 File-Error-Flag Pic X Value Space. 000029 88 File-Error Value “Y”. 000030 01 Name-File-Status Pic XX Value Spaces. 000031 88 Name-File-Success Value “00” “05”. 000032 88 End-Of-File Value “10”. 000033 01 Error-Message Pic X(50) Value Spaces. 000034 Screen Section. 000035 01 Name-Entry Blank Screen. 000036 03 Line 01 Column 01 Value “ Enter Name: ”. 000037 03 Line 01 Column 14 Pic X(30) Using Full-Name. 000038 03 Line 05 Column 01 Pic X(50) From Error-Message. 000039 03 Line 20 Column 01 000040 Value “Press F1 to Exit Press F2 to Update”. 000041 Procedure Division. 000042 Chapt13f-Start. 000043 Perform Open-File 000044 If Not File-Error 000045 Perform Process-File Until F1-Pressed Or 000046 File-Error Or 000047 End-Of-File 000048 Perform Close-File 000049 End-If 000050 Stop Run 000051 . 000052 Open-File. 000053 Open I-O Name-File 000054 If Not Name-File-Success 000055 Move Spaces To Error-Message 000056 String “Open Error ” Name-File-Status 000057 Delimited By Size 000058 Into Error-Message 000059 Perform Display-And-Accept-Error 000060 End-If 000061 . 000062 Process-File. 000063 Move Spaces To Full-Name 000064 Perform Read-File 000065 If Not File-Error 000066 Display Name-Entry 000067 Accept Name-Entry 000068 Move Spaces To Error-Message 000069 If F2-Pressed And Not End-Of-File 000070 Perform Rewrite-Record 000071 End-If 000072 End-If 000073 . 000074 Read-File. 000075 Read Name-File Into Full-Name 000076 At End Move “End Of File” To Error-Message 000077 End-Read 000078 If Name-File-Success Or End-Of-File 000079 Continue 000080 Else 000081 Move Spaces To Error-Message 000082 String “Read Error ” Name-File-Status 000083 Delimited By Size Into Error-Message 000084 End-String 000085 Perform Display-And-Accept-Error 000086 End-If 000087 . 000088 Rewrite-Record. 000089 Rewrite Name-Record From Full-Name 000090 If Name-File-Success 000091 Move “Prior Record Updated” To Error-Message 000092 Else 000093 Move Spaces To Error-Message 000094 String “Rewrite Error ” Name-File-Status 000095 Delimited By Size Into Error-Message 000096 End-String 000097 End-If 000098 . 000099 Display-And-Accept-Error. 000100 Set File-Error To True 000101 Display Name-Entry 000102 Accept Name-Entry 000103 . 000104 Close-File. 000105 Close Name-File 000106 . Take special note of the check for end of file before the attempt at Rewrite. This test prevents you from trying to Rewrite a record after the end of file has been reached. Read carefully through the program. If something is not clear, enter, compile, and run the program in Debug mode and watch what is happening.

Variable-Length Records Sequential data files can contain variable-length records. These data records contain a table that is defined with an Occurs and Depending On. The numeric field that determines the number of occurrences may or may not appear in the data record. If it does appear in the record, it must appear before the table that it helps to define. If it does not, then the Read of the record will not be successful. If the field that determines the number of occurrences is not part of the data record, it must be initialized to the proper number of occurrences before the Read statement is executed. The following example shows a variable-length record, using a table with Occurs and Depending On. You may be limited in the absolute size of a data record. If your maximum number of occurrences exceeds the maximum record size for your system, even if your Depending On number creates a table that keeps your record under the maximum allowable size, your program will not compile.

000020 FD Name-File. 000021 01 Name-Record. 000022 03 Name-Ctr Pic 9(2). 000023 03 Name-Table Occurs 1 to 20 Times Depending On Name-Ctr. 000024 05 Name-Item Pic X(20).

Summary In this hour, you learned the following:

• COBOL has very powerful and simple methods of handling Sequential file Input and Output.

• Files are identified to the COBOL program by using the Select statement and a file description entry, called an FD.

• Before files can be accessed, they must be opened with the Open statement.

• A special field can be defined to capture the status code of any file operation. This field is called the File Status field.

• Records can be created in a file with the use of the Write statement. The file must be opened Output or Extend to use Write.

• When writing to a file, the record description entry is specified, not the filename.

• You can add records to the end of a Sequential file by opening the file Extend and using the Write statement.

• To retrieve data written to a Sequential file, you must Open the file either Input or I-O, and use the Read statement.

• To update records in a Sequential file, Open the file for I-O and use the Rewrite statement. You cannot update records in a Line Sequential file, because the records are variable length.

Q&A Q What is the difference between a Line Sequential file and a Sequential file?

A A Line Sequential file is one kind of Sequential file and is similar to a regular text file. Each record in a Line Sequential file is a line in a file and is delimited by a carriage return and line feed (or just a line feed in the UNIX world). The lines may be of various lengths. Each Read against a Line Sequential file returns a single line as a record. With a regular Sequential file, otherwise known as a Record Sequential file, records are read based on their length. If your records are 80 characters long, every Read returns exactly 80 characters. There are no “lines” and no delimiters separating the records.

Q When I use Optional in my Select statement and I Open the file Input, is it automatically created?

A No. To the program, it looks as if the file is there, but it contains no records. The first Read issued against the file results in an end-of-file condition. The file is not created.

Q If I want to always add records to the end of a file, can I Open Extend even if the file does not exist?

A Yes, but you must specify that the file is Optional on the Select statement. This syntax causes any missing file to be created.

Q I tried to create a Line Sequential file and then open it I-O. The compiler tells me that I can’t do that with a Line Sequential file. Why not?

A The records in a Line Sequential file can be virtually any length. Updating records may involve shortening or lengthening them, which would mean shifting the entire remainder of the file forward and backward to adjust for the size difference. Imagine the overhead and time it would take to accomplish that on large files.

Workshop To help reinforce your understanding of the material presented in this hour, refer to the section “Quiz and Exercise Questions and Answers” that can be found on the CD. This section contains quiz questions and exercises for you to complete, as well as the corresponding answers.

Fair Use Sources

COBOL: COBOL Fundamentals, COBOL Inventor - COBOL Language Designer: 1959 by Howard Bromberg, Norman Discount, Vernon Reeves, Jean E. Sammet, William Selden, Gertrude Tierney, with indirect influence from Grace Hopper, CODASYL, ANSI COBOL, ISO/IEC COBOL; Modern COBOL - Legacy COBOL, IBM COBOL, COBOL keywords, COBOL data structures - COBOL algorithms, COBOL syntax, Visual COBOL, COBOL on Windows, COBOL on Linux, COBOL on UNIX, COBOL on macOS, Mainframe COBOL, IBM i COBOL, IBM Mainframe DevOps, COBOL Standards, COBOL Paradigms (Imperative COBOL, Procedural COBOL, Object-Oriented COBOL - COBOL OOP, Functional COBOL), COBOL syntax, COBOL installation, COBOL containerization, COBOL configuration, COBOL compilers, COBOL IDEs, COBOL development tools, COBOL DevOps - COBOL SRE, COBOL data science - COBOL DataOps, COBOL machine learning, COBOL deep learning, COBOL concurrency, COBOL history, COBOL bibliography, COBOL glossary, COBOL topics, COBOL courses, COBOL Standard Library, COBOL libraries, COBOL frameworks, COBOL research, Grace Hopper, COBOL GitHub, Written in COBOL, COBOL popularity, COBOL Awesome list, COBOL Versions. (navbar_cobol)


© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


sams_teach_yourself_cobol_in_24_hours_-_hour_13_sequential_files.txt · Last modified: 2024/04/28 03:37 by 127.0.0.1