+ All Categories
Home > Documents > File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk,...

File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk,...

Date post: 14-Dec-2015
Category:
Upload: tamsyn-glenn
View: 227 times
Download: 1 times
Share this document with a friend
29
File Structures SNU-OOPSLA Lab. 1 Chap4. Fundamental File Chap4. Fundamental File Structure Concepts Structure Concepts File Structures by Folk, Zoellick, Riccardi
Transcript
Page 1: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 1

Chap4. Fundamental File Chap4. Fundamental File Structure ConceptsStructure Concepts

File Structures by Folk, Zoellick, Riccardi

Page 2: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 2

Chapter ObjectivesChapter Objectives Introduce file structure concepts dealing with

Stream files Reading and writing fields and records Field and record boundaries Fixed-length and variable-length fields and records Packing and unpacking records and buffers

Present an object-oriented approach to file structures Methods of encapsulating object value and behavior in classes

Classes for buffer manipulation Class hierarchy for buffer and file objects and operations Inheritance and virtual functions Template classes

Objectives

Page 3: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 3

ContentsContents

4.1 Field and Record Organization

4.2 Using Classes to Manipulate Buffers

4.3 Using Inheritance for Record Buffer Classes

4.4 Managing Fixed-Length, Fixed-Field Buffers

4.5 An Object-Oriented Class for Record Files

Contents

Page 4: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 4

A Stream fileA Stream file File structure ==> Persistency ==>

Programs outlive the data in files

Simple representation: a file organized as a stream of

bytes Simple, but Reverse Humpty-Dumpty problem

In case of putting all information as a byte

of stream, there is no way to get it apart

Solution : Use field structure

4.1 Field and Record Organization

Page 5: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 5

The Need of Field ConceptThe Need of Field ConceptConsider the function “write Person as a stream of bytes”!

Ostream & operator << (ostream & outputFile, Person & p)

{ // insert (write) fields into stream

outputFile << p.LastName << p.FirstName << p.Address

<< p.City << p.State << p.ZipCode;

return outputFile;

}

(input)

Mary Ames 123 Maple Stillwater, Ok 74074

Alan Mason 90 Eastgate Ada, Ok 74820

(output)

AmesMary123 MapleStillwaterOK74075MasonAlan90 Eastgate…..

Page 6: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 6

Field OrganizationField Organization

Field: The smallest logically meaningful unit of information in a file (not physical)

Field structures (4 methods) Fix the length of fields Begin each field with a length indicator Separate the fields with delimiters Use a “Keyword = value” expression

4.1 Field and Record Organization

Continued

Page 7: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 7

Four methods for organizing filesFour methods for organizing files

Ames John 123 Maple Stillwater OK74075377-1808Mason Alan 90 Eastgate Ada OK74820

(a) Field lengths fixed. Place blanks in the spaces where the phone number would go.

Ames|John|123 Maple|Stillwater|OK|74075|377-1808|Mason|Alan|90 Eastgate|Ada|OK|74820||

(b) Delimeters are used to indicate the end of a field. Place the delimeter for the "empty"fieldimmediately after the delimiter for the previous field.

Ames|...|Stillwater|OK|74075|377-1808|#Mason|... 90Eastgate|Ada|OK|74820|#...

(c) Place the field for business phone at the end of the record. If the end-of-record mark is encountered,assume that the field is missing.

SURNAME=Ames|FIRSTNAME=John|STREET=123 Maple|...|ZIP=74075|PHONE=377-1808|#...

(d) Use a keyword to identify each field each field. If the ketword is missing, the corresponding field isassumed to missing.

4.1 Field and Record Organization

Page 8: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 8

RW files with Field ConceptRW files with Field Concept Extraction operator for delimited fields into a Person object

istream & operator >> (istream & stream, Person & p)

{ // read delimited 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,’|’);

…..

Return stream;}

** By D.5 (pp588) 과 D.7 (pp590)

Last Name ‘Ames’

First Name ‘Mary’

Address ‘123 Maple’

……….

Last Name ‘Mason’

First Name ‘Alan’

……...

Page 9: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 9

Record OrganizationRecord Organization

Record: a set of fields that belong together

Record organization(5 methods) Make records a predictable number of bytes (Fixed-length records) Fig4.5. (a)(b)

Make records a predictable number of fields Fig4.5. (c)

Begin each record with a length indicator Fig4.6. (a)

Use an index to keep track of addresses Fig4.6. (b)

Place a delimiter at the end of each record Fig4.6. (c)

4.1 Field and Record Organization

Page 10: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 10

The method for organizing records (1)The method for organizing records (1)

Three ways of making the lengths of records constant and predictable Fixed-length record w/ fixed-length fields

Fixed-length record w/ variable-length fields

Six fields per record

Ames John 123 Maple Stillwater OK74075

Mason Alan 90 Eastgate Ada OK74820

Ames|John|123 Maple|Stillwater|OK|74075|

Mason|Alan|90 Eastgate|Ada|OK|74820|

Unused space

Unused space

Ames|John|123 Maple|Stillwater|OK|74075| Mason|Alan|90 Eastgate|Ada|OK| . . . .

(a)

(b)

(c)

4.1 Field and Record Organization

Fig. 4.5

Page 11: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 11

The method for organizing records (2)The method for organizing records (2)

Record structure for variable record with a length indicator using a index file with delimiter(#)

Ames|John|123 Maple|Stillwater|OK|74075|Mason|Alan|90 Eastgate . . .

Ames|John|123 Maple|Stillwater|OK|74075|Mason|Alan . . .

Ames|John|123 Maple|Stillwater|OK|74075|#Mason|Alan|90 Eastgate|Ada|OK . . .

00 40 . . .

Data file:

Index file:

(a)

(b)

(c)

4.1 Field and Record Organization

Fig. 4.6

Page 12: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 12

Write a var-length delimited buffer to a fileWrite a var-length delimited buffer to a file(from memory to disk)(from memory to disk)

Const int MaxBUfferSize = 200;

int WritePerson(ostream & stream, Person & p)

{char buffer [MaxBufferSize];

strcpy(buffer, p.LastName); strcat(buffer, “l”);

strcpy(buffer, p.FistName); strcat(buffer, “I”);

…..

Strcpy(buffer,p.Zipcode); strcat(buffer, “l”);

short length=strlen(buffer);

stream.write (&length, sizeof(length));

stream.write(&buffer, length)

}

Figure 4.7 (pp 129)

Page 13: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 13

Reading Variable RecordsReading Variable Records Records preceded by lengths (variable length records)

40 Ames|Mary|123 Maple|Stillwater|OK|74075|

36 Mason|Alan|90 Eastgate|Ada|OK|74820

int ReadVariablePerson (istream & stream, Person & p)

{ // read a variable sized record from stream and store it in p

short length;

stream . Read (&length, sizeof(lenth));

char * buffer = new char[length + 1]; // create a buffer space

stream . Read (buffer, length);

buffer [ length] = 0; // treminate buffer with null

istrstream strbuff (buffer); // create a string stream

strbuff >> p; // use the istream extraction operator

return 1;

}

Page 14: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 14

Read-file using File DumpRead-file using File Dump File-dump gives us the ability to look inside a file at the actual

bytes that are stored Octal Dump: od -xc filename e.g. The number 40, stored as ASCII characters and as a short

integer

(a) 40 stored as ASCII chars:

(b) 40 stored as a 2-byte integer:

Decimal value ofnumber

40

40

Hex value storedin bytes

ASCIIcharacter form

'4' '0'

'\0' "("

4.1 Field and Record Organization

34 30

00 28

Page 15: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 15

Using Classes to Manipulate BuffersUsing Classes to Manipulate Buffers Examples of three C++ classes to encapsulate

operation of buffer object Function : Pack, Unpack, Read, Write

Output: pack into a buffer & write a buffer to a file Input: read into a buffer from a file & unpack a buffer ‘pack and unpack’ deals with only one field

DelimTextBuffer class for delimited fields LengthTextBuffer class for length-based fields FixedTextBuffer class for fixed-length fields

Appendix E : Full implementation

4.2 Using Classes to Manipulate Buffers

Page 16: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 16

Buffer Class for Delimited Text Fields(1)Buffer Class for Delimited Text Fields(1)

Variable-length buffer Fields are represented as delimited text

4.2 Using Classes to Manipulate Buffers

Class DelimTextBuffer{ public:

DelimTextBuffer (char Delim = ‘|’, int maxBtytes = 1000);

int Read(istream & file);int Write (ostream & file) const;int Pack(const char * str, int size = -1);int Unpack(char * str);

private:char Delim; // delimiter characterchar * Buffer; // character array to hold field

valuesint BufferSize; // current size of packed fieldsint MaxBytes; // maximum # of characters in the

bufferint NextByte; // packing/unpacking position in

buffer};

Page 17: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 17

Buffer Class for Delimited Text Fields(2)Buffer Class for Delimited Text Fields(2)

int DelimTextBuffer::Unpack(char *str)// extract the value of the next field of the buffer{

int len = -1; // length of packed stringint start = NextByte; // first character to be unpackedfor(int i = start; i < BufferSize; i++)

if(Buffer[i] == Delim){len = i-start; break;}

if(len == -1) return FALSE; // delimiter not foundNextByte += len + 1;if(NextByte > BufferSize) return FALSE;strncpy (str, &Buffer[start], len);str[len] = 0; // zero termination for stringreturn TRUE;

}

Unpack() is extracking one field from a record in a buffer.Pack() method copies the characters of its argument to the buffer and then

adds the delimiter characters.

4.2 Using Classes to Manipulate Buffers

Page 18: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 18

Buffer Class for Delimited Text Fields(3)Buffer Class for Delimited Text Fields(3)

Read method of DelimTextBuffer Clears the current buffer contents Extracts the record size Read the proper number of bytes into buffer Set the buffer size

int DelimTextBuffer::Read(istream & stream){

Clear();stream.read((char *)&BufferSize, sizeof(BufferSize));if (Stream.fail()) return FALSE;if (BubberSize > MaxBytes) return FALSE; // buffer overflowstream.read(Buffer, BufferSize);return stream.good();

}

4.2 Using Classes to Manipulate Buffers

Page 19: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 19

Extending Class Person with Buffer OperationsExtending Class Person with Buffer Operations

class Person{public:char lastname[11]; char firstname[11]; …char zipcode[10];

// method…int Pack(DelimTextBuffer &buf) const; // buffer operation Pack...

}

int Person::Pack(DelimTextBuffer &buf) const{ // pack the fields into a DelimTextBuffer

int result;result = buf.Pack(lastname); result = result && buf.Pack(firstname);…return result = result && buf.Pack(zipcode);

}

* pack deals with only one field!

4.2 Using Classes to Manipulate Buffers

Page 20: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 20

Buffer Class for Delimitted Text Field(Reminder)Buffer Class for Delimitted Text Field(Reminder)

Class DelimTextBuffer{ public:

DelimTextBuffer (char Delim = ‘|’, int maxBtytes = 1000);int Read(istream & file);int Write (ostream & file) const;int Pack(const char * str, int size = -1);int Unpack(char * str);

private:char Delim; // delimiter characterchar * Buffer; // character array to hold field valuesint BufferSize; // current size of packed fieldsint MaxBytes; // maximum # of characters in the bufferint NextByte; // packing/unpacking position in buffer

};

Page 21: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 21

Buffer Classes for Length-Based FieldsBuffer Classes for Length-Based Fields

Almost same as the delimited field class (compare with the previous page)

Change in the implementations of the Pack and Unpack

class LengthTextBuffer{ public:

LengthTextBuffer(int maxBytes = 1000);int Read(istream & file);int Write(ostream & file) const;int Pack(const char * field, int size = -1);int Unpack(char * field);

private:char * Buffer; // character array to hold field valuesint BufferSize; // size of packed fieldsint MaxBytyes; // maximum # of characters in the bufferint NextByte; // packing/unpacking position in buffer

};

4.2 Using Classes to Manipulate Buffers

Page 22: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 22

Buffer Classes for Fixed-length FieldsBuffer Classes for Fixed-length Fields

Class FixedTextBuffer{ public:

FixedTextBuffer (int maxBytes = 1000);int AddField (int fieldSize);int Read(isteram * file);int Write(ostream *file) const;int Pack(const char * field);int Unpack (char * field);

private:char * Buffer; // character array to hold field valuesint BufferSize; // size of packed fieldsint MaxBytes; // Max # of chars in the bufferint NextByte; // packing/unpacking position in bufferint * FieldSizes; // array of field sizes

}

4.2 Using Classes to Manipulate Buffers

Page 23: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 23

Inheritance in the C++ Stream ClassesInheritance in the C++ Stream Classesclass istream: virtual public ios { …class ostream: virtual public ios { …class iostream: virtual istream, public ostream { …class ifstream: public fstreambase, public istream { …class ostream: public fstreambase, public ostream {…class fstream: public fstreambase, public iostream { …

Operations that work on base class objects also work on derived class objects

4.3 Using Inheritance for Record Buffer Classes

Page 24: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 24

Class Hierarchy for Record Buffer Objects(1)Class Hierarchy for Record Buffer Objects(1)

4.3 Using Inheritance for Record Buffer Classes

D elim ited F ieldB uff erp ac k an d un p ac k o p eratio n s

fo r delim ited fi eld s

L en g thF ield B uff erp ac k an d un p ac k o p eratio n s

fo r len g th- b as ed fi eld s

Variab leL en g thB uff erread an d write o p eratio n sfo r variab le len g th rec o rd s

F ixed F ieldB uff erp ac k an d un p ac k o p eratio n s fo r

fi xed s ized fi eld s

F ixed L en g thB uff erread an d write o p eratio n s

fo r fi xed len g th rec o rds

IO B uff erc har array fo r b uff er value

Appendix F : full implementation

Inheritance allows multiple classes share members and methods

Page 25: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 25

Class Hierarchy for Record Buffer Objects(2)Class Hierarchy for Record Buffer Objects(2)

class IOBuffer{ public:

IOBuffer (int maxBytes = 1000); // a MAX of maxBytevirtual int Read (istream &) = 0; // read a buffervirtual int Write (ostream &) = 0; // write a buffervirtual int Pack (const void * field, int size = -1) = 0;virtual int Unpack (void * field, int maxbytes = -1) = 0;

protected:char * Bufffer; // character array to hold field valuesint BufferSize; // sum of the sizes of packed fieldsint MaxBytes; // MAX # of characters in the buffer

};

4.3 Using Inheritance for Record Buffer Classes

Page 26: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 26

Class Hierarchy for Record Buffer Objects(3)Class Hierarchy for Record Buffer Objects(3)

Class VariableLengthBuffer: public IOBuffer{ public:

VariableLengthBuffer (int MaxBytes = 1000);int Read (istream &);int Write (ostream &) const;int SizeOfBuffer () const; // return current size of buffer

};

class DelimFieldBuffer: public VariableLengthBuffer{ public:

DelimFieldBuffer (char Delim = -1, int maxBytes = 1000);int Pack (const void *, int size = -1);int Unpack (void *field, int maxBytes = -1);

protected:char Delim;

};

4.3 Using Inheritance for Record Buffer Classes

Page 27: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 27

Managing Fixed-Length, Fixed-Field BuffersManaging Fixed-Length, Fixed-Field Buffers

class FixedFieldBuffer: public FixedLengthBuffer{ public:

FixedFieldBuffer (int maxFields, int RecordSzie = 1000);FixedFieldBuffer (int maxFields, int *fieldSize);int AddField (int fieldSize); // define the next fieldint Pack(const void * field, int size = -1);int Unpack(void * field, int maxBytes = -1);int NumberOfFields () const; // return # of defined fields

protected:int * FieldSzie; // array to hold field sizesint MaxFields; // MAX # of fieldsint NumFields; // actual # of defined fields

};

4.4 Managing Fixed-Length, Fixed_Field Buffers

Page 28: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 28

Object-Oriented Class for Record FilesObject-Oriented Class for Record Files So far, we defined buffer classes Now, we encapsulate all of our file operations!

class BufferFile // file with buffers{ public:

BufferFile (IOBuffer &); // create with a buffer int Open(char * fname, int MODE); // open an existing file

int Create (char * fname, int MODE); // create a new fileint Close();int Rewind(); // reset to the first data record// Input and Output operationsint Read(int recaddr = -1);int Write(int recaddr = -1);int Append(); // write the current buffer at the end of file

protected:IOBuffer & Buffer; // reference to the file’s bufferfstream File; // the C++ stream of the file

};

Usage: DelimFieldBuffer buffer; BufferFile file(buffer);

file.open(myfile); file.Read(); buffer.Unpack(myobject);

4.5 An Object-Oriented Class for Record Files

Page 29: File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts File Structures by Folk, Zoellick, Riccardi.

File Structures SNU-OOPSLA Lab. 29

Let’s Review!!!Let’s Review!!!

4.1 Field and Record Organization

4.2 Using Classes to Manipulate Buffers

4.3 Using Inheritance for Record Buffer Classes

4.4 Managing Fixed-Length, Fixed-Field Buffers

4.5 An Object-Oriented Class for Record Files

Contents


Recommended