sams_teach_yourself_cobol_in_24_hours_-_hour_16_updating_indexed_file_records

Sams Teach Yourself COBOL in 24 Hours - Hour 16 Updating Indexed File Records

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)

Hour 16 Updating Indexed File Records

A large part of business programming involves updating records in data files. Information is constantly changing, and the files containing this information require frequent updates. In the examples using the dealer file, you are keeping up with information such as address and telephone number that could change. To keep these files current, records have to be updated. Updating an Indexed file record is fairly simple. In this hour, you learn about updating Indexed files and also about working with Relative files. The following topics are covered:

• Opening a file for I-O (simultaneous Input and Output)

• Writing records

• Rewriting records

• Deleting records

• Working with Relative files; similarities and differences with Indexed files

Opening for I-O You know that when you Open a file Output, you are creating a new file. The only records it contains are those written after the Open statement. Any records that may have been in the file are lost. If you want to add records to a file, you know that you can Open the file Extend. To retrieve records from a file, you Open it Input. Updating records in an Indexed file requires that you be able to Read a record so that you can present its contents to the user for modification. You also need to be able to Write the new record, or update the existing record if a change has been made. Opening for Output or Extend won’t accomplish either of these goals.

COBOL provides an Open mode that allows you to Read records from an Indexed file, Write new records, and update existing records. This Open mode is I-O, meaning Input-Output. The File Status values returned when opening a file I-O are the same as those returned when opening the file for Input.

All the statements used when reading from an Indexed file that is Open Input apply when the file is Open I-O. You may specify Sequential, Random, or Dynamic access. You may Read records from the file as if it were Open Input. You may Write new records to the file using Write as if the file were Open Output. (There are some special considerations for writing to an Indexed file Open I-O when the Organization is Sequential— these are discussed shortly.) Finally, you may update existing records.

Writing Records One way to update an Indexed file is to add new records to the file with the Write statement. The way the Write statement works depends on the access method used and Open mode of the file. Remember that access can be Sequential, Random, or Dynamic.

You may not Open the file I-O with Sequential access mode and Write records. If you attempt to do so, a File Status of 48 is returned. This status is returned because you cannot Write to an Indexed file with Sequential access that is opened for I-O. If you need to add a record to an Indexed file with Sequential access, you should Open the file for Extend instead of I-O. When writing to the file, you must ensure that the primary Key of the record being written is greater than the last record in the file. If not, you receive a File Status 21, record out of sequence, error. The primary Key controls the sequence of records for Sequential Write operations. Keep in mind that although Sequential access is pretty fast and has its place, you probably don’t want to try to add records to an Indexed file Open in Sequential mode.

When you use Random access and have the file Open I-O, you may Write new records to the file. The order of the writes does not matter. If a duplicate primary Key is encountered, a File Status of 22 is returned. Your new record does not replace the existing record. If you have Alternate Record Keys and duplicates are not allowed—but one is encountered during the Write—a File Status 22 is also returned. In this case, it is not possible to determine which Key is being duplicated by the Write: the primary or one of the Alternate Key fields. If duplicates are allowed on the Alternate Key and one is encountered as the result of a Write statement, a File Status of 02 is returned.

Another potential File Status value that can be returned from this type of operation is a 24. This value means that you have attempted to Write outside the externally defined boundary of the file. Focus on the term externally defined. Some systems define Indexed files outside the program. The files must be defined to the operating system, and specific size limitations may apply. In this case, if the maximum size of the file is exceeded, a File Status 24 is returned.

Remember that any File Status that begins with 2, such as 22, is also an Invalid Key condition and that you may code the Invalid Key clause with the Write. The COBOL standard requires that you either code the Invalid Key clause or have Declaratives defined for the file when performing a Write against an Indexed file.

For the Write statement, Dynamic and Random access work the same way. Writes to an Indexed file that is Open I-O with Dynamic access are identical to writes to a file that is Open with Random access. You may not Open an Indexed file with Dynamic access for Extend.

The Write statement is coded the same as with Sequential files. You specify the record description to be written, not the file name. The only additional check that may be coded with Indexed files is the Invalid Key clause.

000101 Write Dealer-Record From Dealer-Work 000102 Invalid Key Perform Invalid-Dealer-Write 000103 End-Write Remember that the From, when used with Write, moves the contents of Dealer-Work into the record area defined by Dealer-Record.

Rewriting Records One very useful feature of Indexed files and COBOL is the capability to update an existing data file record. The Rewrite statement replaces an existing record with a new record. This statement is especially useful for updating information that is likely to change or changes frequently. When an address changes for one of your dealers, you update the dealer file by rewriting that dealer’s record. The behavior of the Rewrite statement depends on the type of access you have selected for the file. The Rewrite statement is available only when the file is Open I-O.

When the file is defined with Sequential access, the Rewrite statement overlays the last record read with the new data record. You should be aware of some issues.

First, a Rewrite can be performed only if the last statement executed against the file was a Read statement, and the Read was successful. If you attempt to Rewrite a record when access is Sequential, without first reading a record, the Rewrite fails. The File Status reported for this failure is 43, which simply means that the last statement executed for the file was not a successful Read. This failure occurs even after a successful Rewrite statement if you attempt to execute another Rewrite without first performing a Read.

Second, when rewriting a record you may not change the primary Key. For this example, that means you cannot Read the dealer file, change the dealer’s number, and then Rewrite the record. Any attempt to do so results in a File Status 21 for record out of sequence.

Image

When using Indexed files, regardless of the access mode specified, you cannot change the primary Key. Think about the implications for your programs if this practice were allowed. You might have a purchase file that matches merchandise to dealers. In this file, rather than keeping all of the information relating to the dealer, you just keep the dealer number (for file maintenance). If a dealer’s address changes, all you have to do is change the dealer file. Because the purchase file refers only to the dealer number, nothing needs to change in it. Now imagine what would happen if you were allowed to change the dealer number in the dealer master file. Your number in the purchase file would not be changed! This condition would break the link between the dealer file and the purchase file. You might use that dealer number again, and then the purchase file would point to the wrong dealer! Any time you feel you must change the primary Key of an Indexed file, you need to consider all the places in your system that might use the Key. You may then add the record with your new primary Key and Delete the one with the old primary Key. (Deleting records is discussed in the “Deleting Records” section in this hour.)

When Random or Dynamic access is selected for the Indexed file, the Rewrite statement becomes less restrictive. The Rewrite does not have to be preceded by a successful Read statement. The primary Key determines the placement of the record in the file. When you execute the Rewrite statement, if the primary Key does not exist for the record you are rewriting, then the File Status is set to 23—record not found—and an Invalid Key condition occurs. If you Rewrite a record and cause a duplicate Alternate Key condition, a File Status of 22 is returned and an Invalid Key condition occurs.

Image

I suggest that you always Read the record that you are going to Rewrite. Because the Rewrite statement allows you to replace a record without regard to its contents, erasing information in your data record with a Rewrite is relatively easy. Imagine that you want to change a dealer’s phone number. If you fill in a record with dealer number and phone number and then Rewrite the record, all of the other information in the record is lost. It is replaced by the values that you may have had initialized or have left over from a previous, but unrelated, Read statement. The best practice is to Read the record, move in the fields being updated, and then Rewrite the record.

The act of rewriting a record does not change the current record positioning in the file. Therefore, you can change the contents of a record, even changing the Alternate Key value by which you are reading. The next record you Read is then based on what that Key used to be and not the new value. For example, if you are reading the dealer file by Alternate Key value and you change a last name from “Smith” to “Jones”, your next Read returns the record after “Smith”, not the record after “Jones”.

The following snippet of code reads a record, moves in replacement values, and then rewrites that record. The Invalid Key clause on the Rewrite statement is coded to either catch records that do not exist and disallowed duplicate Alternate Key values.

000201 Move “A1366” To Dealer-Number Of Dealer-Record 000202 Read Dealer-File 000203 Invalid Key Move “Dealer Not Found” To Error-Message 000204 Set File-Error To True 000205 End-Read 000206 If Not File-Error 000207 Move “(909) 555-1212” To Home-Phone Of Dealer-Record 000208 Rewrite Dealer-Record 000209 Invalid Key Move Spaces To Error-Message 000210 String “Error Rewriting Dealer File ” Dealer-Status 000211 Delimited By Size Into Error-Message 000212 Set File-Error To True 000213 End-Rewrite 000214 End-If Image

It should be obvious that you would never code a telephone number update with a literal in a program, as was done in the preceding example. The example is for illustration only. In reality, this telephone number would come from user input or a transaction record. Coding items such as this, using a literal, is known as hard coding. This term describes something that is coded in the program to always occur. Imagine the frustration of trying to debug the preceding situation if you left this code in a program that actually accepted and updated telephone numbers. No matter what the user entered, dealer number A1366 would always have the same telephone number!

Deleting Records On occasion you may wish to remove, or delete, records from your Indexed files. Like the Rewrite statement, the Delete statement is valid only when the file is Open I-O. The Delete statement removes the records from the file. The primary Key of the file is the determining factor in deleting a record. Unlike the Write and Rewrite statements, when a Delete statement is coded, the filename is specified, for example:

000215 Delete Dealer-File When Organization is Sequential, the record deleted is the last record read. The Delete statement is valid only when the last operation against the file is a successful Read statement. If not, the Delete returns a File Status value of 43. Because a Delete cannot return File Status values beginning with a 2 when the file is Open with Sequential access, coding Invalid Key on such a Delete is not allowed.

When Dynamic or Random access is selected for the file, the Delete statement, like the Rewrite, becomes a little less restrictive. The record being deleted need not have been previously read. Simply fill in the primary Key information in the record description for the file and issue the Delete statement. If the record does not exist, a File Status of 23 is returned and an Invalid Key condition exists. You may code the Invalid Key clause on a Delete statement if the access mode of the file against which the Delete is being processed is Random or Dynamic.

Image

Some programmers fall into the trap of thinking that they can Delete records via the Alternate Key. They fill in the Alternate Key information and then issue the Delete statement. This technique does not work. The Delete statement applies only to the record identified by the primary Key of the data file from which it is to be deleted.

Issuing a Delete statement does not disturb the file positioning if you are reading sequentially through an Indexed file. The next Read follows the previously issued Read statement.

Relative Files Relative files are cousins to Indexed files. Relative files behave like Indexed files except the Primary Key for the file is not part of the data record and there are no Alternate keys. Relative files are keyed by a Relative record number. The first record in a Relative file is record 1. A Relative file is like a giant array against which you may perform Indexed file-type operations. The Select statement for the Relative file defines its Organization and tells the program the name of the data field in Working-Storage that contains the record number that is the Key for the file.

Select Rel-File Assign To “Relative.Dat”

               Organization Relative
               Access Dynamic
               Relative Key Is Rel-Work-Num
               File Status Rel-Status.
The field that defines the Key is specified with the Relative Key clause, not the Record Key clause as was the case for an Indexed file. The Relative Key can be any unsigned integer data item.

When using Sequential access for a Relative file and writing records, the Key of the record just written is stored in the field identified in the Select statement as the Relative Key. You must take care to make your Relative Key field large enough to handle the greatest number of records you expect to have in the file. If the field is too small and you attempt to Write a record whose Key value exceeds the maximum for the file, an Invalid Key condition occurs and the File Status is set to 24.

When reading a Relative file with Sequential access, the Relative Key of the record just read is stored in the Relative Key field. If the Key for the last record read exceeds the maximum value that your field can hold, an At End condition occurs and the File Status value is set to 14.

The short program in Listing 16.1 illustrates Relative files and their use.

Listing 16.1 Relative File Access Example

000001 @OPTIONS MAIN,TEST 000002 Identification Division. 000003 Program-Id. Chapt16a. 000004* Relative File Access Example 000005 Environment Division. 000006 Configuration Section. 000007 Special-Names. 000008 Crt Status Is Keyboard-Status 000009 Cursor Is Cursor-Position. 000010 Source-Computer. IBM-PC. 000011 Object-Computer. IBM-PC. 000012 Input-Output Section. Because the file is assigned using Optional, it will be created if it does not exist. Notice the use of Organization Relative and the Relative Key clause. Note also that Relative-Key is not a field in the data record. There is nothing special about the name Relative-Key. It was chosen because the name describes the field’s purpose. You could easily name the field, Record-Number, Relative-Record-Number, or Field-A.

000014 Select Optional Relative-File Assign To “Relative.Dat” 000015 Organization Relative 000016 Access Dynamic 000017 Relative Key Relative-Key 000018 File Status Relative-Status. 000019 Data Division. 000020 File Section. 000021 Fd Relative-File. 000022 01 Relative-Record. 000023 03 Relative-Data Pic X(20). Relative-Key is defined in Working-Storage as a two-character unsigned numeric field. Because the size is limited to two digits, the maximum Relative Key the file can have is 99.

000024 Working-Storage Section. 000025 01 Relative-Key Pic 99 Value Zeros. 000026 01 Relative-Status Pic X(2) Value Spaces. 000027 88 Relative-Success Value “00” Thru “09”. 000028 01 Keyboard-Status. 000029 03 Accept-Status Pic 9. 000030 03 Function-Key Pic X. 000031 88 F1-Pressed Value X“01”. 000032 88 F2-Pressed Value X“02”. 000033 88 F3-Pressed Value X“03”. 000034 88 F4-Pressed Value X“04”. 000035 88 F5-Pressed Value X“05”. 000036 88 F6-Pressed Value X“06”. 000037 88 F7-Pressed Value X“07”. 000038 88 F8-Pressed Value X“08”. 000039 03 System-Use Pic X. 000040 01 Cursor-Position. 000041 03 Cursor-Row Pic 9(2) Value 1. 000042 03 Cursor-Column Pic 9(2) Value 1. 000043 01 Error-Message Pic X(50) Value Spaces. The following table creates the initial Relative file if the program detects that no such file exists.

000044 01 Table-Area. 000045 03 Table-Values. 000046 05 Filler Pic X(20) Value “Entry 1”. 000047 05 Filler Pic X(20) Value “Entry 2”. 000048 05 Filler Pic X(20) Value “Entry 3”. 000049 05 Filler Pic X(20) Value “Entry 4”. 000050 05 Filler Pic X(20) Value “Entry 5”. 000051 05 Filler Pic X(20) Value “Entry 6”. 000052 05 Filler Pic X(20) Value “Entry 7”. 000053 05 Filler Pic X(20) Value “Entry 8”. 000054 05 Filler Pic X(20) Value “Entry 9”. 000055 05 Filler Pic X(20) Value “Entry 10”. 000056 03 Load-Table Redefines Table-Values. 000057 05 Basic-Table Pic X(20) Occurs 10 Times. 000058 Screen Section. 000059 01 Data-Entry-Screen 000060 Blank Screen, Auto 000061 Foreground-Color Is 7, 000062 Background-Color Is 1. 000063* 000064 03 Screen-Literal-Group. 000065 05 Line 01 Column 25 Value “Relative File Example” 000066 Highlight Foreground-Color 4 Background-Color 1. 000067 05 Line 4 Column 01 Value “Current Relative Key: ”. 000068 05 Line 5 Column 01 Value “Relative Data: ”. 000069 05 Line 22 Column 01 Value “F1-Read Random Number”. 000070 05 Line 22 Column 23 Value “F2-Start Number”. 000071 05 Line 22 Column 56 Value “F3-Read Next Number”. 000072 05 Line 23 Column 01 Value “F4-Delete Record”. 000073 05 Line 23 Column 23 Value “F5-Write Record”. 000074 05 Line 23 Column 56 Value “F6-Rewrite Record”. 000075 05 Line 24 Column 01 Value “F7-Clear”. 000076 05 Line 24 Column 23 Value “F8-Exit”. 000077 03 Required-Reverse-Group Reverse-Video. 000078 05 Line 4 Column 23 Pic 9(2) Using Relative-Key. 000079 05 Line 5 Column 16 Pic X(25) Using Relative-Data. 000080 05 Line 20 Column 01 Pic X(50) From Error-Message. 000081* 000082 Procedure Division. The Invalid Key clause captures errors whose status value begins with a 2. Declaratives are coded to capture any other errors that might occur, such as attempting a sequential Read after the end of file is reached.

000083 Declaratives. 000084 Relative-File-Error Section. 000085 Use After Standard Error Procedure On Relative-File 000086 . 000087 Relative-Error. 000088 String “Error on Relative.Dat ” 000089 Relative-Status 000090 Delimited By Size 000091 Into Error-Message 000092 End-String 000093 . 000094 End Declaratives. This next segment of code performs the Open of the file and, if it is successful, continues to the processing loop, where the user interacts with the program.

000095 Chapt16a-Start. 000096 Perform Open-File 000097 If Not Relative-Success 000098 String “Error Opening Relative File” 000099 Relative-Status 000100 Delimited By Size 000101 Into Error-Message 000102 End-String 000103 Move Spaces To Relative-Data 000104 Perform Display-And-Accept 000105 Else 000106 Move Spaces To Relative-Data 000107 Perform Process-File Until F8-Pressed 000108 Perform Close-File 000109 End-If 000110 Stop Run 000111 . The processing loop continues to execute until the user presses the F8 key. Each time through the loop, the key pressed is tested and the appropriate action is performed.

000112 Process-File. 000113 Perform Display-And-Accept 000114 Evaluate True 000115 When F1-Pressed 000116 Perform Read-Random-Number 000117 When F2-Pressed 000118 Perform Start-Number 000119 When F3-Pressed 000120 Perform Read-Next-Number 000121 When F4-Pressed 000122 Perform Delete-Number 000123 When F5-Pressed 000124 Perform Write-Record 000125 When F6-Pressed 000126 Perform Rewrite-Record 000127 When F7-Pressed 000128 Perform Clear-Screen 000129 When F8-Pressed 000130 Continue 000131 When Other 000132 Continue 000133 End-Evaluate 000134 . The Read-Random-Number paragraph performs a random Read against the Relative file. You can tell that this is a random Read and not a sequential Read because there is no Next. The record whose Relative record number matches that of the Relative-Key field is returned.

000135 Read-Random-Number. 000136 Read Relative-File 000137 Invalid Key 000138 String “Error on Random Read Number” 000139 Relative-Status 000140 Delimited By Size 000141 Into Error-Message 000142 End-Read 000143 . As with an Indexed file, the Start statement positions the file for the next Read. Start, Read, Write, Rewrite, and Delete can be used in this program because Dynamic access was specified on the Select statement. What do you think would happen if you tried to Start the file with this Start statement and a Relative Key value of zeros? That Key value can’t exist in a Relative data file. The program will try to position the file on this Key value but it will fail. An Invalid Key condition will exist with a File Status value of 23.

000144 Start-Number. 000145 Start Relative-File Key = Relative-Key 000146 Invalid Key 000147 String “Start Error Number” 000148 Relative-Status 000149 Delimited By Size 000150 Into Error-Message 000151 Not Invalid Key 000152 String “Start Successful” 000153 Relative-Status 000154 Delimited By Size 000155 Into Error-Message 000156 End-Start 000157 . The Read statement with Next returns the next record in the file. If you just did the Start as coded in this program, the record specified by that Start is returned, not the one after it as you might expect. Remember that Start only positions the file and does not return a record.

000158 Read-Next-Number. 000159 Read Relative-File Next 000160 At End 000161 Move “End of File” To Error-Message 000162 End-Read 000163 . Delete removes the Relative record whose Relative record number is set in the Relative-Key field. Be aware that if the fifth record of the file is deleted in this method, the sixth record does not become the fifth, and so on. The result is a missing record in the Relative file. If you try to do a random Read, Delete, or Rewrite on this record now, you receive a File Status 23. If you Write the record again, it is created in its previous physical location in the file.

000164 Delete-Number. 000165 Delete Relative-File 000166 Invalid Key 000167 String “Delete Error” 000168 Relative-Status 000169 Delimited By Size 000170 Into Error-Message 000171 Not Invalid Key 000172 Move “Record Deleted” To Error-Message 000173 Perform Clear-Screen 000174 End-Delete 000175 . The Write statement adds a record to the Relative file whose Relative record number is that of the Relative-Key field. Some performance issues are related to the use of Relative files. For example, if your Relative file contained records where the highest Relative record number was 50 and you wrote a record with Relative record number 1,000,050, the system would have to reserve the space for one million records between Relative record 50 and Relative record 1,000,050. This can take quite a long time.

If the record you are writing already exists, a File Status of 22 is returned and an Invalid Key condition exists.

000176 Write-Record. 000177 Write Relative-Record 000178 Invalid Key 000179 String “Write Error” 000180 Relative-Status 000181 Delimited By Size 000182 Into Error-Message 000183 Not Invalid Key 000184 Move “Write Successful” 000185 To Error-Message 000186 End-Write 000187 . The Rewrite statement replaces the record whose Relative record number is the number in Relative-Key. If you attempt to Rewrite a Relative record number that does not exist, a File Status 23 is returned.

000188 Rewrite-Record. 000189 Rewrite Relative-Record 000190 Invalid Key 000191 String “Rewrite Error” 000192 Relative-Status 000193 Delimited By Size 000194 Into Error-Message 000195 Not Invalid Key 000196 Move “Rewrite Successful” 000197 To Error-Message 000198 End-Rewrite 000199 . The Clear-Screen and Display-And-Accept paragraphs help with the interface to the user. Clear-Screen is executed in two places: after the F7 key is pressed and during the process of creating the file so that the last Record Key is not left on the display.

000200 Clear-Screen. 000201 Initialize Relative-Record 000202 Move Zeros To Relative-Key 000203 Move 01 To Cursor-Row Cursor-Column 000204 . 000205 Display-And-Accept. 000206 Display Data-Entry-Screen 000207 Accept Data-Entry-Screen 000208 Move Spaces To Error-Message 000209 . The Open statement opens the file for Input and Output. Because the file was made Optional, it is created the first time it is opened if it does not already exist. A File Status of 05 is returned, which tells the program to create some base records so that you don’t have to. In this case, the program creates 10 records, using a table defined in Working-Storage.

000210 Open-File. 000211 Open I-O Relative-File 000212 If Relative-Status = “05” 000213 Perform Create-Base-File Varying Relative-Key 000214 From 1 By 1 000215 Until Relative-Key > 10 Or 000216 Not Relative-Success 000217 Perform Clear-Screen 000218 End-If 000219 . The Create-Base-File paragraph creates 10 records in the Relative file. This step gives you a simple file to work with when you run the program.

000220 Create-Base-File. 000221 Write Relative-Record From Basic-Table (Relative-Key) 000222 Invalid Key 000223 String “Creation Write Error” 000224 Relative-Status 000225 Delimited By Size 000226 Into Error-Message 000227 Perform Display-And-Accept 000228 End-Write 000229 . The Close is coded to release the file to the operating system.

000230 Close-File. 000231 Close Relative-File 000232 . Enter, compile, link, and try running this program. As you run the program, scroll forward through the file until you reach the end. Then press the F3 key again to Read another record. Notice that File Status 46 is returned. This status value means that a sequential Read was attempted, but no next record is established—the end of the file has been reached! If Declaratives are not coded to capture this error, you might not know about it.

Experiment with deleting different Relative record numbers and adding new ones. Try deleting a record and then writing a new record to the same Relative Key. Try starting on a Relative record number that you have deleted. Experiment with the different things you can do and observe the results. Do things happen the way you expect? If not, why not? If necessary, follow the program with the debugger and observe what is happening inside the program.

Summary In this hour, you learned the following:

• You may update an Indexed file by opening the file I-O or Extend. Opening Extend limits you to adding new records. To update existing records, you must Open the file I-O.

• When using Sequential access, you can Rewrite or Delete a record only when the last operation for the file is a successful Read statement.

• When using Dynamic or Random access, you can Delete or Rewrite a record without first reading it. You must use caution when performing a Rewrite in this manner so as not to erase information in the record that you want to keep.

• Write statements return a File Status 22 if a record already exists in the file with the same primary Key or Alternate Key that does not allow duplicates.

• If a Rewrite or Delete is executed for a record that does not exist, a File Status of 23 is returned.

• Unlike Write and Rewrite, the Delete statement is coded with the filename, not the record description name, as the identifier for the operation.

• Relative files are similar to Indexed files. The difference is that the Key is always a Relative record number, and the field that contains this number is not a part of the data record. The Key field is identified in the Select statement as a Relative Key instead of a Record Key.

• Deleting Relative records does not cause the remaining records to be renumbered. Instead, the location where that record was located is cleared, and another record with the same Relative record number may be written in that place.

• Declaratives can be useful for capturing file errors that are missed by the coding of the Invalid Key or At End condition.

Q&A Q When I want to update an Indexed file, can I Open it with Sequential access, or must I Open it with Random or Dynamic?

A You may Open the file with Sequential access. Depending on the updates being applied, Sequential may be the most efficient access method. You have to remember that you cannot perform random reads when the file is Open with Sequential access. Records may be rewritten only after a successful Read.

Q I understand Relative files, but I can’t think of a good use for them. Can you give me some examples?

A You can use a Relative file as an alternative to a table when you don’t know the number of records that will be in the table. This use is not always very efficient as far as access time goes, but is a viable solution. Obviously, you will have to code your own Search of the file, as you cannot use the Search verb against a Relative file. I have also used Relative files for storing Key values when paging through an Indexed file. This method allows me to display a page of data, saving the Key field for the first item on the screen in a Relative file. Then if the user wants to page backward or return to a specific page, I use the page number as the Relative Key to read the Relative file and find the Indexed file Record Key that last started that page.

Q Can I Open a regular Record Sequential file and address it as if it were a Relative file?

A You must be very careful because only a few compiler vendors allow this technique. Usually, some compiler-dependent internal record identifier is associated with a Relative file, and becomes part of the record. Opening a Record Sequential file as a Relative file can cause problems and will most likely result in a File Status 39.

Q When writing to a Relative file Open with Sequential access, I get a File Status 24. What does that mean?

A It means that your Relative Record Key field is too small. If it is defined as Pic 9(2) and you attempt to write the 100th record, you receive a File Status 24.

Q I know that my Relative file has more than 1,000 records, but I can only Read the first 999. Then I get an At End condition. How can this be?

A You must be accessing the file with Sequential access. Your Relative Record Key field is too small. When the maximum value it can hold is reached when reading a file, a status 14 is returned, which is a valid At End condition. Increase the size of your Relative Record Key field.

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)


Cloud Monk is Retired (for now). Buddha with you. © 2005 - 2024 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_16_updating_indexed_file_records.txt · Last modified: 2022/05/16 03:23 by 127.0.0.1