120 likes | 231 Views
Chapter 4 Fundamental File Structure Concepts. Objectives: To get familiar with Alternatives in field and record organizations Object-oriented approach to buffered I/O. Outline. Field organization Record organization A record structure that uses a length indicator
E N D
Chapter 4 Fundamental File Structure Concepts Objectives: To get familiar with Alternatives in field and record organizations Object-oriented approach to buffered I/O
Outline • Field organization • Record organization • A record structure that uses a length indicator • Using classes to manage buffers • A class hierarchy for record buffers
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.
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?
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 Mary|Ames|123 Maple|Stillwater|OK|74075| Alan|Mason|90 Eastgate|Ada|OK|74820| Keywords (with delimiters) first=Mary|last=Ames|address=123 Maple|city=Stillwater|state=OK|zip=74075| first=Alan|last=Mason|address=90 Eastgate|city=Ada|state=OK|zip=74820|
istream & operator >> (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’ Reading Delimited Fields
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 file’s 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.
A Record Structure that Uses a Length Indicator • Writing the variable-length records to the file length indicator at the beginning of a record --> 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
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.
A Class Hierarchy for Record Buffers IOBuffer char array for buffer value VariableLengthBufferFixedLengthBuffer read and write operations read and write operations for variable length records for fixed length records DelimitedFieldBuffer LengthFieldBuffer FixedFieldBuffer pack and unpack pack and unpack pack and unpack operations for operations for operations for delimited fields length-based fields fixed sized fields