Welcome to this crucial unit in our COBOL File Handling course. In this lesson, we will cover the basics of creating and writing to a sequential file. This skill is essential, especially in environments like banking, where capturing and storing account details accurately is paramount.
In this lesson, you will learn how to create a new sequential file and write data into it using COBOL. Understanding this process is foundational for handling files, one of the core tasks in COBOL programming.
Here’s a glimpse of what the code will look like:
cobol1IDENTIFICATION DIVISION. 2PROGRAM-ID. WriteToFile. 3ENVIRONMENT DIVISION. 4INPUT-OUTPUT SECTION. 5 6FILE-CONTROL. 7 SELECT OutputFile ASSIGN TO 'accounts.dat' 8 ORGANIZATION IS SEQUENTIAL. 9 10DATA DIVISION. 11FILE SECTION. 12 13FD OutputFile. 1401 OutputRecord. 15 05 Customer-Name PIC X(30). 16 05 Account-Number PIC 9(10). 17 18PROCEDURE DIVISION. 19 OPEN OUTPUT OutputFile. 20 MOVE "John Doe" TO Customer-Name. 21 MOVE 1234567890 TO Account-Number. 22 23 WRITE OutputRecord. *> Will write "John Doe 1234567890" to the `accounts.dat` file 24 CLOSE OutputFile. 25 STOP RUN.
Let's break down the code snippet above:
ENVIRONMENT DIVISION
: This division is primarily responsible for specifying the computer environment. In addition, it defines the input and output sources for the program using the INPUT-OUTPUT SECTION
and FILE-CONTROL
.
INPUT-OUTPUT SECTION.
: This section defines the files and the necessary information needed to access them within the program. The section has a FILE-CONTROL
paragraph that defines the files used in the program.FILE-CONTROL
: Here, we define the variable for the file using the SELECT
statement. Notice the ORGANIZATION IS SEQUENTIAL
clause, which specifies that the file is organized sequentially. In this case, we have a file named OutputFile
that is assigned to the accounts.dat
file.DATA DIVISION
: This division defines the data structures used in the program.
FILE SECTION
: This section defines the file structure. We have an FD
entry for the OutputFile
file, which contains the record structure OutputRecord
with two fields: Customer-Name
and Account-Number
.In the PROCEDURE DIVISION
, we open the OutputFile
file for writing using the OPEN OUTPUT OutputFile
statement – this will create a new file if it doesn't already exist or open an existing file for writing and position the file pointer at the beginning. The content of the file will be overwritten if it already exists.
We then move values to the Customer-Name
and Account-Number
fields of the OutputRecord
record.
Finally, we write the OutputRecord
record to the OutputFile
file using the WRITE OutputRecord
statement. This will write the record to the file and advance the file pointer to the next record.
Handling files is a cornerstone of COBOL programming, especially in sectors like finance where the persistent storage of data is crucial. By learning how to write to a sequential file, you’ll gain the ability to create programs that can store customer information systematically, ensuring data integrity and reliability.
Whether you're looking to build robust banking applications or any other system that requires data storage, mastering file handling will significantly enhance your programming toolkit. Understanding this will also give you the groundwork for more advanced file operations like reading, updating, and deleting records, which we will cover in subsequent units.
Feeling excited? Let’s dive in and start the practice section to get hands-on experience with writing to a sequential file in COBOL!