1 / 27

Chapter 7

Chapter 7. File I/O. File, Record & Field. File, Record & Field. The file is just a chunk of disk space set aside for data and given a name. The computer has no idea what kind of data is in a file. E.g. james.doc, bal.xls These are sometimes called physical files . File, Record & Field.

miach
Download Presentation

Chapter 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7 File I/O

  2. File, Record & Field

  3. File, Record & Field • The file is just a chunk of disk space set aside for data and given a name. • The computer has no idea what kind of data is in a file. • E.g. james.doc, bal.xls • These are sometimes called physical files.

  4. File, Record & Field • In a COBOL program, a file is a collection of related units of information within a data category. • A file might contain all the information (related units of information) about customers (a data category) for a company. • This usually is called a data file or a logical file. • For the logical file to exist, there must be a physical file on the disk. • When the bytes in this physical file are arranged logically so that a COBOL program can access the information, it becomes a data file to a COBOL program.

  5. File, Record & Field • Within a data file, the information about one unit is called a record. • If a data file contains the information pertaining to all customers, for example, the information about one customer is a record. • A field or data field is one piece of data contained in a record.

  6. Defining a File in COBOL • In a COBOL program, the description of a data record is entered as a structure variable. • When dealing with files, all the variables associated with a file, including the variables in the record, start with an identical prefix.

  7. Defining a File in COBOL • In COBOL, a file is defined in two parts: • Logical file, which includes the record layout, • Physical file, which includes the name of the file on the disk and how the file will be organized. • A logical file is easy to define after you define the record. • A logical file is defined in the DATA DIVISION, in a new section called the FILE SECTION.

  8. Defining a File in COBOL

  9. Defining a File in COBOL • the FILE SECTION appears before the WORKING-STORAGE section in the DATA DIVISION. • FD - A logical file definition is called a file descriptor. • given the special level number FD.

  10. Defining a File in COBOL • The variable name used for a file does not truly name a variable; it really is a file identifier. You cannot • move anything to PHONE-FILE, and if you tried to, the compiler probably would generate an error. You • do need to open and close a file in order to be able to read data from it and write data to it, and • The filename identifier is used with the OPEN and CLOSE commands to open and close the file.

  11. Defining a File in COBOL • The LABEL RECORDS ARE STANDARD clause is a holdover from the days of processing files on tape. • Some recent COBOL compilers have recognized that much more processing these days is done on disk, and the LABEL RECORDS clause is optional. • Should include the clause to keep your COBOL compatible across different machines and COBOL compilers.

  12. Defining a File in COBOL • The physical description of the file fits in the ENVIRONMENT DIVISION. • The physical description of a COBOL file is a set of statements designed to identify the physical name of the file on the disk drive, and how the file is organized. • The ENVIRONMENT DIVISION is reserved to provide information on the physical computer on which COBOL is running.

  13. Defining a File in COBOL • Different types of computers have different ways of naming files on a disk, so it makes sense that this information is placed in the ENVIRONMENT DIVISION. • A physical file description is placed in the FILE-CONTROL paragraph in the INPUT-OUTPUT SECTION of the ENVIRONMENT DIVISION.

  14. Defining a File in COBOL • The INPUT-OUTPUT SECTION is a reserved name for the section of the ENVIRONMENT DIVISION that is used to define physical files used in a program and to define which areas of memory will be used by the files while they are being processed. • The FILE-CONTROL paragraph is a reserved name for a paragraph in the INPUT-OUTPUT SECTION of the ENVIRONMENT DIVISION, and it is used to define all the physical files used in a program.

  15. Defining a File in COBOL

  16. Defining a File in COBOL • The SELECT clause uses the logical filename (PHONE-FILE) and associates it with a physical filename on the disk by using the ASSIGN TO "phone.dat“. • SEQUENTIAL organization indicates that records in the file are processed in sequence. When a record is written to the file, it always is appended to the end of the file. • When a file is opened and read, the first read retrieves the first record and each subsequent read retrieves each next record.

  17. Opening and Closing the file • Before you can do anything to the contents of a file, you must open the file. When you open a file, you have to specify an OPEN mode. • A mode indicates the type of processing that you intend to do on the file. • E.g. • OPEN OUTPUT PHONE-FILE • OPEN EXTEND PHONE-FILE • OPEN INPUT PHONE-FILE • OPEN I-O PHONE-FILE

  18. Opening and Closing the file

  19. Opening and Closing the file • Notice that files opened for INPUT or I-O can cause an error if the file does not physically exist on the disk. • Closing a file is a lot less complicated; you simply use the reserved word CLOSE. It doesn't matter what the original open mode was. • E.g. • CLOSE PHONE-FILE

  20. Add records to file • Adding a record to a file involves writing a record. • E.g.WRITE file-record • WRITE PHONE-RECORD • DO ensure that if more than one program accesses the same file, both programs use the same SELECT, and an FD of the same length to describe the file. • DON'T try to open a file with a SELECT or FD that is different from the SELECT or FD used to create the file. There are special cases where you can do this, but they are used in data conversions. • DON'T change the record layout of a file by adding fields that change the length of the record without also re-creating the data file.

  21. Reading record from a File • A file to be opened for reading should be opened in INPUT mode with the following syntax: • READ filename NEXT RECORD. • Here is an example: • READ PHONE-FILE NEXT RECORD.

  22. Reading record from a File • SELECT clause for the file says that it is organized as a SEQUENTIAL file (for the example in this slide), every READ on the file is a request to retrieve the next record. • Each READ reads the next record in the file. • There actually are two types of read for a file: • READ • READ NEXT. • Because a SEQUENTIAL file is organized to be accessed one record after another, a READ and READ NEXT have the same effect on a SEQUENTIAL file.

  23. Reading record from a File • How do you know when all the records in a file have been read? • If you don't find out, your program will keep attempting to read the next record when nothing is left in the file to read. • The READ NEXT statement can include a clause that is executed if the file has reached the end.

  24. Reading record from a File • DO ensure that whenever a file is read sequentially using READ NEXT, some flag is set to indicate the end of a file. • DON'T ignore the possible end-of-file condition by leaving out a test for it in the main logic • of the program.

  25. File processing loop

  26. File processing loop • the first record in the file is read before the processing loop starts. • the processing loop is performed until the end of the file. • three key pieces to use when processing a file from one end to the other: • Set a flag that keeps track of whether the file is at the end to reflect a "not at end" condition and read the first record. • Perform the processing loop until the file is at end. • Read the next record at the bottom of the processing loop.

  27. Summary • Refer to text page 214 Summary. • If you add a field to the PHONE-RECORD to include the extension and recompile the program, will the original file phone.dat process correctly using the new program?

More Related