Objectives: - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Objectives:

Description:

Goal: encapsulate the pack, unpack, read, write operations of ... record from input stream, then unpack field values one by one. ... pack and unpack operations ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 13
Provided by: chuahua
Category:

less

Transcript and Presenter's Notes

Title: Objectives:


1
Chapter 4 Fundamental File Structure Concepts
  • Objectives
  • To get familiar with
  • Alternatives in field and record organizations
  • Object-oriented approach to buffered I/O

2
Outline
  • Field organization
  • Record organization
  • A record structure that uses a length indicator
  • Using classes to manage buffers
  • A class hierarchy for record buffers

3
Field
  • A field is the smallest logically meaningful unit
    of information in a file.
  • A record is a collection of fields which belong
    to an object.
  • A field does not need to exist in any physical
    sense, but keeping the identity of the fields is
    important. A stream file that loses the track of
    fields makes the information unusable.
  • For example
  • Mary Ames 123 Maple Stillwater, OK 74075
  • Alan Mason 90 Eastgate Ada, OK 74820
  • A stream of bytes
  • AmesMary123 MapleStillwater, OK 74075MasonAlan90
    EastgateAda, OK 74820
  • Problem There is no way to get the information
    back in the organized record format.

4
Field Organization
  • Ways to maintain the identity of fields
  • Force the field into a predictable length
  • fixed-length
  • Begin each field with a length indicator
  • Place a delimiter at the end of each field to
    separate it from the next field
  • Use a keyword value expression to identify
    each field and its content
  • Choose an organization for fields
  • Space utilization
  • Data characteristics fixed-length or variable
    length
  • Delimiter must not appear in file
  • Addressing is it easy to determine the start
    position a field?

5
Examples of Field Organization
  • Fixed Length
  • Mary Ames 123 Maple
    Stillwater OK 74075
  • Alan Mason 90 Eastgate Ada
    OK 74820
  • Length Indicator
  • 04Mary04Ames09123 Maple10Stillwater02OK0574075
  • 04Alan05Mason1190 Eastgate03Ada02OK0574820
  • Delimiters
  • MaryAmes123 MapleStillwaterOK74075
  • AlanMason90 EastgateAdaOK74820
  • Keywords (with delimiters)
  • firstMarylastAmesaddress123
    MaplecityStillwaterstateOKzip74075
  • firstAlanlastMasonaddress90
    EastgatecityAdastateOKzip74820

6
Reading Delimited Fields
  • istream operator gtgt (istream stream, Person
    p)
  • // read fields from file
  • char delim
  • stream.getline(p.LastName, 30,'')
  • if (strlen(p.LastName)0) return stream
  • stream.getline(p.FirstName,30,'')
  • stream.getline(p.Address,30,'')
  • stream.getline(p.City, 30,'')
  • stream.getline(p.State,15,'')
  • stream.getline(p.ZipCode,10,'')
  • return stream
  • This time, we do preserve the notion of fields,
    but something is missing Rather than a stream of
    fields, these should be two records
  • Last Name Ames
  • First Name Mary
  • Address 123 Maple
  • City Stillwater
  • State OK
  • Zip Code 74075
  • Last Name Mason
  • First Name Alan
  • Address 90 Eastgate
  • City Ada
  • State OK
  • Zip Code 74820

7
Record Organization
  • A record can be defined as a set of fields that
    belong together when the file is viewed in terms
    of a higher level of organization.
  • Like the notion of a field, a record is another
    conceptual tool which needs not exist in the file
    in any physical sense.
  • Yet, they are an important logical notion
    included in the files structure.
  • Methods for organizing the records of a file
    include
  • Requiring that the records be a predictable
    number of bytes in length.
  • fixed-length records
  • Requiring that the records be a predictable
    number of fields in length.
  • Beginning each record with a length indicator
    consisting of a count of the number of bytes (or
    number of fields) that the record contains.
  • Using a second file to keep track of the
    beginning byte address for each record.
  • Placing a delimiter at the end of each record to
    separate it from the next record.

8
(No Transcript)
9
(c)
10
A Record Structure that Uses a Length Indicator
  • Writing the variable-length records to the file
  • length indicator at the beginning of a record --gt
    know the sum of field length before writing
    record to file
  • Use a buffer
  • Representing the record length
  • Binary
  • Text
  • Be careful of byte order
  • Reading the variable-length record from the file

11
Using Classes to Manage Buffers
  • Goal encapsulate the pack, unpack, read, write
    operations of buffers
  • Usage
  • Output start with an empty buffer object, pack
    field values into the object, then write buffer
    to output stream.
  • Input initialize a buffer object by reading a
    record from input stream, then unpack field
    values one by one.
  • Constraints
  • No updates on packed data
  • No mixing of pack and unpack operations
  • Design approach look at concrete classes first,
    then abstract out a base class.

12
A Class Hierarchy for Record Buffers
  • IOBuffer
  • char array for buffer value
  • VariableLengthBuffer FixedLengthBuffer
  • read and write operations read and write
    operations
  • for variable length records for fixed length
    records
  • DelimitedFieldBuffer LengthFieldBuffer FixedFieldB
    uffer
  • pack and unpack pack and unpack pack and
    unpack
  • operations for operations for operations for
  • delimited fields length-based fields fixed sized
    fields
Write a Comment
User Comments (0)
About PowerShow.com