Lesson 3
Appending Records to a Sequential File
Appending Records to a Sequential File

Welcome back! You've already learned how to create, write to, and read from a sequential file in COBOL. Now, it's time to take another essential step: appending records to an existing sequential file. This lesson is critical for applications that need to continually add new data without altering or overwriting the existing information.

What You'll Learn

In this lesson, you will master how to append records to a sequential file in COBOL. Appending records allows you to add new data to the end of an existing file. Understanding this capability is especially useful in scenarios such as updating customer information or logging transactions, where data needs to be stored continuously.

Here's a snippet of the code you'll be working with:

cobol
1IDENTIFICATION DIVISION. 2PROGRAM-ID. AppendToFile. 3ENVIRONMENT DIVISION. 4INPUT-OUTPUT SECTION. 5FILE-CONTROL. 6 SELECT OutputFile ASSIGN TO 'accounts.dat' 7 ORGANIZATION IS SEQUENTIAL. 8DATA DIVISION. 9FILE SECTION. 10FD OutputFile. 1101 OutputRecord. 12 05 Customer-Name PIC X(10). 13 05 Account-Number PIC 9(10). 14 05 Account-Balance PIC 9(8)V99. 15PROCEDURE DIVISION. 16 OPEN EXTEND OutputFile. 17 MOVE "Jane Doe" TO Customer-Name. 18 MOVE 9876543210 TO Account-Number. 19 MOVE 2200.50 TO Account-Balance. 20 WRITE OutputRecord. 21 CLOSE OutputFile. 22 STOP RUN.

As previously we used the same logic to define the file and the record structure. The only difference is that we are using the OPEN EXTEND statement to open the file in append mode. This mode allows you to add new records to the end of the file without overwriting the existing data.

In this example, we are appending a new record to the accounts.dat file. The record contains the customer's name, account number, and account balance. After opening the file in append mode, we write the new record to the file and close it.

If we run the code multiple times, we will keep appending new records to the file without losing the previously stored data, for example if we run it twice, the file will contain two records as follows:

1Jane Doe 98765432100000220050Jane Doe 98765432100000220050

Notice, that the number 2200.50 is stored as 0000220050 in the file. This is because we are using the PIC 9(8)V99 format to store the account balance, hence the leading zeros. And, in addition to that, the dot is not stored in the file.

In addition, pay attention that when we are writing in the file twice, the content of the file will be concatenated without any separation between the records.

Why It's Important

Appending records to a file is crucial for maintaining accurate and up-to-date data logs. Whether you’re working on banking software, customer management systems, or data collection applications, knowing how to append records can help you:

  • Log continuous transactions
  • Update customer information over time
  • Maintain audit trails for data integrity

By mastering this skill, you will ensure that your applications can grow and adapt without losing previously stored data.

Sounds interesting, right? Let's move on to the practice section to get some hands-on experience with appending records to a sequential file in COBOL.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.