Presentation is loading. Please wait.

Presentation is loading. Please wait.

File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts 서울대학교 컴퓨터공학부 객체지향시스템연구실 SNU-OOPSLA-LAB 김 형 주 교수 File Structures by Folk, Zoellick,

Similar presentations


Presentation on theme: "File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts 서울대학교 컴퓨터공학부 객체지향시스템연구실 SNU-OOPSLA-LAB 김 형 주 교수 File Structures by Folk, Zoellick,"— Presentation transcript:

1 File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts 서울대학교 컴퓨터공학부 객체지향시스템연구실 SNU-OOPSLA-LAB 김 형 주 교수 File Structures by Folk, Zoellick, Riccardi

2 File StructuresSNU-OOPSLA Lab.2 Chapter Objectives u Introduce file structure concepts dealing with u Stream files u Reading and writing fields and records u Field and record boundaries u Fixed-length and variable-length fields and records u Packing and unpacking records and buffers u Present an object-oriented approach to file structures u Methods of encapsulating object value and behavior in classes u Classes for buffer manipulation u Class hierarchy for buffer and file objects and operations u Inheritance and virtual functions u Template classes Objectives

3 File StructuresSNU-OOPSLA Lab.3 Contents 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

4 File StructuresSNU-OOPSLA Lab.4 A Stream file u File structure ==> Persistency ==> Programs outlive the data in files u Simple representation: a file organized as a stream of bytes u Simple, but Reverse Humpty-Dumpty problem u In case of putting all information as a byte of stream, there is no way to get it apart u Solution : Use field structure 4.1 Field and Record Organization

5 File StructuresSNU-OOPSLA Lab.5 The Need of Field Concept Consider 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…..

6 File StructuresSNU-OOPSLA Lab.6 Field Organization u Field: The smallest logically meaningful unit of information in a file (not physical) u 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

7 File StructuresSNU-OOPSLA Lab.7 Four methods for organizing files Ames John 123 Maple Stillwater OK74075377-1808 Mason 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"field immediately 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 is assumed to missing. 4.1 Field and Record Organization

8 File StructuresSNU-OOPSLA Lab.8 RW files with Field Concept u 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’ ……...

9 File StructuresSNU-OOPSLA Lab.9 Record Organization u Record: a set of fields that belong together u 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

10 File StructuresSNU-OOPSLA Lab.10 The method for organizing records (1) u Three ways of making the lengths of records constant and predictable u Fixed-length record w/ fixed-length fields u Fixed-length record w/ variable-length fields u Six fields per record 4.1 Field and Record Organization Fig. 4.5

11 File StructuresSNU-OOPSLA Lab.11 The method for organizing records (2) u Record structure for variable record u with a length indicator u using a index file u with delimiter(#) 4.1 Field and Record Organization Fig. 4.6

12 File StructuresSNU-OOPSLA Lab.12 Write a var-length delimited buffer to a file (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)

13 File StructuresSNU-OOPSLA Lab.13 Reading Variable Records u 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; }

14 File StructuresSNU-OOPSLA Lab.14 Read-file using File Dump u File-dump gives us the ability to look inside a file at the actual bytes that are stored u Octal Dump: od -xc filename u 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 of number 40 Hex value stored in bytes ASCII character form '4''0' '\0'"(" 4.1 Field and Record Organization 34 30 00 28

15 File StructuresSNU-OOPSLA Lab.15 Using Classes to Manipulate Buffers u Examples of three C++ classes to encapsulate operation of buffer object u Function : Pack, Unpack, Read, Write u Output: pack into a buffer & write a buffer to a file u Input: read into a buffer from a file & unpack a buffer u ‘pack and unpack’ deals with only one field u DelimTextBuffer class for delimited fields u LengthTextBuffer class for length-based fields u FixedTextBuffer class for fixed-length fields u Appendix E : Full implementation 4.2 Using Classes to Manipulate Buffers

16 File StructuresSNU-OOPSLA Lab.16 Buffer Class for Delimited Text Fields(1) u Variable-length buffer u 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 character char * Buffer; // character array to hold field values int BufferSize;// current size of packed fields int MaxBytes;// maximum # of characters in the buffer int NextByte;// packing/unpacking position in buffer };

17 File StructuresSNU-OOPSLA Lab.17 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 string int start = NextByte; // first character to be unpacked for(int i = start; i < BufferSize; i++) if(Buffer[i] == Delim) {len = i-start; break;} if(len == -1) return FALSE;// delimiter not found NextByte += len + 1; if(NextByte > BufferSize) return FALSE; strncpy (str, &Buffer[start], len); str[len] = 0;// zero termination for string return 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

18 File StructuresSNU-OOPSLA Lab.18 Buffer Class for Delimited Text Fields(3) u Read method of DelimTextBuffer u Clears the current buffer contents u Extracts the record size u Read the proper number of bytes into buffer u 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 overflow stream.read(Buffer, BufferSize); return stream.good(); } 4.2 Using Classes to Manipulate Buffers

19 File StructuresSNU-OOPSLA Lab.19 Extending 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

20 File StructuresSNU-OOPSLA Lab.20 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 character char * Buffer; // character array to hold field values int BufferSize;// current size of packed fields int MaxBytes;// maximum # of characters in the buffer int NextByte;// packing/unpacking position in buffer };

21 File StructuresSNU-OOPSLA Lab.21 Buffer Classes for Length-Based Fields u Almost same as the delimited field class (compare with the previous page) u 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 values int BufferSize; // size of packed fields int MaxBytyes; // maximum # of characters in the buffer int NextByte;// packing/unpacking position in buffer }; 4.2 Using Classes to Manipulate Buffers

22 File StructuresSNU-OOPSLA Lab.22 Buffer 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 values int BufferSize; // size of packed fields int MaxBytes; // Max # of chars in the buffer int NextByte;// packing/unpacking position in buffer int * FieldSizes; // array of field sizes } 4.2 Using Classes to Manipulate Buffers

23 File StructuresSNU-OOPSLA Lab.23 Inheritance in the C++ Stream Classes class 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 { … u Operations that work on base class objects also work on derived class objects 4.3 Using Inheritance for Record Buffer Classes

24 File StructuresSNU-OOPSLA Lab.24 Class Hierarchy for Record Buffer Objects(1) 4.3 Using Inheritance for Record Buffer Classes Appendix F : full implementation  Inheritance allows multiple classes share members and methods

25 File StructuresSNU-OOPSLA Lab.25 Class Hierarchy for Record Buffer Objects(2) class IOBuffer { public: IOBuffer (int maxBytes = 1000); // a MAX of maxByte virtual int Read (istream &) = 0; // read a buffer virtual int Write (ostream &) = 0; // write a buffer virtual 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 values int BufferSize; // sum of the sizes of packed fields int MaxBytes; // MAX # of characters in the buffer }; 4.3 Using Inheritance for Record Buffer Classes

26 File StructuresSNU-OOPSLA Lab.26 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

27 File StructuresSNU-OOPSLA Lab.27 Managing 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 field int 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 sizes int MaxFields; // MAX # of fields int NumFields; // actual # of defined fields }; 4.4 Managing Fixed-Length, Fixed_Field Buffers

28 File StructuresSNU-OOPSLA Lab.28 Object-Oriented Class for Record Files u So far, we defined buffer classes u 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 file int Close(); int Rewind();// reset to the first data record // Input and Output operations int 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 buffer fstream 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

29 File StructuresSNU-OOPSLA Lab.29 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


Download ppt "File StructuresSNU-OOPSLA Lab.1 Chap4. Fundamental File Structure Concepts 서울대학교 컴퓨터공학부 객체지향시스템연구실 SNU-OOPSLA-LAB 김 형 주 교수 File Structures by Folk, Zoellick,"

Similar presentations


Ads by Google