Programming Logic and Design: Ninth Edition

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 50

Programming Logic and Design

Ninth Edition

Chapter 7
File Handling and Applications

Programming Logic and Design, Ninth Edition 1


Objectives
In this chapter, you will learn about:
• Computer files
• The data hierarchy
• Performing file operations
• Control break logic
• Merging files
• Master and transaction file processing
• Random access files

Programming Logic and Design, Ninth Edition 2


Understanding Computer Files
• Computer file
– A collection of data stored on permanent storage devices
such as your computer’s hard drive, a hard drive on the
cloud, DVDs, USB drives, and reels of magnetic tape
– Text files (numbers, names, salaries) that can be read by a
text editor
– Binary files (images and music) not encoded as text

Programming Logic and Design, Ninth Edition 3


Understanding Computer Files (continued -1)

• Computer files have:


– A filename - an identifying name given to a computer file
that frequently describes the contents
• JanuaryPayroll
• PreviousMonthSales
– A filename extension - a group of characters added to the
end of a filename that describes the type of the file
• .txt
• .dat
• .docx

Programming Logic and Design, Ninth Edition 4


Understanding Computer Files (continued -2)

• Computer files have:


– A specific creation time and modification date
– A file size measured in bytes
• byte – one character
• kilobyte – thousands of bytes
• megabyte – millions of bytes
• gigabyte – billions of bytes
• terabyte – trillions of bytes

Programming Logic and Design, Ninth Edition 5


Understanding Computer Files (continued -3)

Programming Logic and Design, Ninth Edition 6


Understanding Computer Files (continued -4)

• Organizing files
– Directories and folders
• Organization units on storage devices
– Path
• Combination of disk drive plus the complete hierarchy of
directories
• Example:
C:\Logic\SampleFiles\PayrollData.dat

Programming Logic and Design, Ninth Edition 7


Understanding the Data Hierarchy
• Data hierarchy
– Describes the relationships between data components
– Consists of:
• Characters – letters number and special symbols
• Fields – data items representing a single attribute of a record
• Records – groups of fields that go together for some logical
reason
• Files – groups of related records
• Database – holds related file data in tables

Programming Logic and Design, Ninth Edition 8


Understanding the Data Hierarchy (continued)

Programming Logic and Design, Ninth Edition 9


Performing File Operations
• File operations to use data files in your programs
– Declare a file identifier
InputFile employeeData
OutputFile updatedData
– Open the file
open employeeData "EmployeeData.dat"
– Reading from a file and processing the data
input name from employeeData
input address from employeeData
input payRate from employeeData

Programming Logic and Design, Ninth Edition 10


Performing File Operations (continued -1)

• Reading from a file and processing the data


– Programming languages have different ways of
determining how much data to input
– In many languages, a delimiter such as a comma,
semicolon, or tab character is stored between data fields

Programming Logic and Design, Ninth Edition 11


Performing File Operations (continued -2)

Programming Logic and Design, Ninth Edition 12


Performing File Operations (continued -3)

Programming Logic and Design, Ninth Edition 13


Performing File Operations (continued -4)

• Sequential file
– Program reads all the records in this file from beginning to
end, processing them one t a time
• Sorting
– The process of placing records in order by the value in a
specific field or fields
• Ascending order – records sorted in order from lowest to
highest values
• Descending order – records sorted in order from highest to
lowest values

Programming Logic and Design, Ninth Edition 14


Performing File Operations (continued -5)

• Writing data to a file


– When you store data in a computer file on a persistent
storage device, you write to the file
• output name, address, payRate to employeeData
• Closing a file
– When you finish using a file, the program should close the
file
– Always close every file you open
• Default input and output devices (keyboard and
monitor) do not require opening or closing

Programming Logic and Design, Ninth Edition 15


A Program that Performs File
Operations
•Backup file
– a copy kept in case values need to be restored to their
original state

– The backup copy is called a parent file

– The newly revised copy is a child file

Programming Logic and Design, Ninth Edition 16


A Program that
Performs File
Operations (continued -1)

Programming Logic and Design, Ninth Edition 17


A Program that
Performs File
Operations (continued -2)

Programming Logic and Design, Ninth Edition 18


Understanding Control Break Logic
• A control break is a temporary detour in the logic of
a program
– A control break program uses a change in a value to
initiate special actions or processing
– A control break report groups similar data together
• Input records must be in sequential order

Programming Logic and Design, Ninth Edition 19


Understanding Control Break Logic (continued -1)

Programming Logic and Design, Ninth Edition 20


Understanding Control Break Logic (continued -2)

• Examples of control break reports


– All employees listed in order by department number, with
a new page started for each department
– All books for sale in a bookstore listed in order by
category (such as reference or self-help), with a count
following each category of book
– All items sold in order by date of sale, with a different ink
color for each new month

Programming Logic and Design, Ninth Edition 21


Understanding Control Break Logic (continued -3)

• Single-level control break


– A detour based on the value of a single variable
– Uses a control break field to hold the previous value

Programming Logic and Design, Ninth Edition 22


Understanding
Control Break
Logic (continued -4)

Programming Logic and Design, Ninth Edition 23


Understanding
Control Break
Logic (continued -5)

Programming Logic and Design, Ninth Edition 24


Understanding Control Break Logic (continued -6)

Programming Logic and Design, Ninth Edition 25


Merging Sequential Files
• Merging files
– Combining two or more files while maintaining the
sequential order or the records
• Examples
– A file of current employees in ID number order, and a file
of newly hired employees also in ID number order
– A file of parts manufactured in the Northside factory in
part-number order, and a file of parts manufactured in the
Southside factory also in part-number order

Programming Logic and Design, Ninth Edition 26


Merging Sequential Files (continued -1)

• Two conditions required for merging files


– Each file has the same record layout
– Sorted in the same order based on the same field

Programming Logic and Design, Ninth Edition 27


Merging Sequential Files (continued -2)

Programming Logic and Design, Ninth Edition 28


Merging Sequential Files (continued -3)

• Mainline logic similar to other file-processing


programs, except for handling two files
• With two input files, must determine when both files
are at eof
– Define a flag variable to indicate that both files have
reached eof
– Must define two input files
– Read one record from each input file

Programming Logic and Design, Ninth Edition 29


Merging Sequential Files (continued -4)

Programming Logic and Design, Ninth Edition 30


Merging
Sequential
Files (continued -5)

Programming Logic and Design, Ninth Edition 31


Merging
Sequential
Files (continued -6)

Programming Logic and Design, Ninth Edition 32


Merging Sequential Files (continued -7)

Programming Logic and Design, Ninth Edition 33


Merging Sequential Files (continued -8)

Programming Logic and Design, Ninth Edition 34


Merging Sequential Files (continued -9)

Programming Logic and Design, Ninth Edition 35


Master and Transaction File
Processing
• Some related files have a master-transaction
relationship
• Master file
– Holds complete and relatively permanent data
• Transaction file
– Contains temporary data to be used to update the master
file
• Update the master file
– Changes to values in its fields based on transactions

Programming Logic and Design, Ninth Edition 36


Master and Transaction File
Processing (continued -1)

• Examples
– A library maintains a master file of all patrons and a
transaction file with information about each book or other
items checked out
– A college maintains a master file of all students and a
transaction file for each course registration
– A telephone company maintains a master file of every
telephone line (number) and a transaction file with
information about every call

Programming Logic and Design, Ninth Edition 37


Master and Transaction File
Processing (continued -2)

• Updating approaches
– Change information in master file
– Copy master file and change new version
• Begin with both files sorted in the same order on the
same field

Programming Logic and Design, Ninth Edition 38


Master and
Transaction
File
Processing(continued -3)

Programming Logic and Design, Ninth Edition 39


Master and
Transaction
File
Processing(continued -4)

Programming Logic and Design, Ninth Edition 40


Master and
Transaction
File
Processing(continued -5)

Programming Logic and Design, Ninth Edition 41


Master and
Transaction
File
Processing(continued -6)

Programming Logic and Design, Ninth Edition 42


Master and Transaction File
Processing (continued -7)

Programming Logic and Design, Ninth Edition 43


Master and Transaction File
Processing (continued -8)

Programming Logic and Design, Ninth Edition 44


Random Access Files
• Batch processing
– Involves performing the same tasks with many records,
one after the other
– Uses sequential files
• Real-time applications
– Require that a record be accessed immediately while a
client is waiting
• Interactive program
– A program in which the user makes direct requests

Programming Logic and Design, Ninth Edition 45


Random Access Files (continued -1)

• Random access files


– Records can be physically located in any order
– Instant access files
• Files in which records must be accessed immediately
– Also known as direct access files

Programming Logic and Design, Ninth Edition 46


Random Access Files (continued -2)

Programming Logic and Design, Ninth Edition 47


Summary
• Computer file
– A collection of data stored on a nonvolatile device in a
computer system
• Data items are stored in a hierarchy
• To use a data file you must declare, open, read, write,
and close the file
• Sequential file: records stored one after another in
some order

Programming Logic and Design, Ninth Edition 48


Summary (continued -1)

• Control break program reads a sorted sequential file


and performs special processing based on a change
in one or more fields in each record in the file
• Merging files combines two or more files
– Maintains the same sequential order
• Master files
– Hold permanent data
– Updated by transaction files

Programming Logic and Design, Ninth Edition 49


Summary (continued -2)

• Master files
– Hold relatively permanent data
– Updated by transaction files
• Real-time applications
– Require random access files where records can be located
in any order
• Instant access files and direct access files are files in
which records must be accessed immediately

Programming Logic and Design, Ninth Edition 50

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy