Sie sind auf Seite 1von 16

File Organization File Access

File Structures

4.1 Field and Record Organization

A 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. Weve lost the integrity of the fundamental organizational units of our input data. Solution : Use field structure

File Structures

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..

File Structures

Field 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

File Structures

Four methods for organizing fields


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

(a) Field lengths fixed.


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. If the keyword is missing, the corresponding field is assumed to missing. File Structures 5

Definition of record to hold person information


struct Person{ char last[11]; char first[11]; char address[16]; char city[16]; char state[3]; char zip[10]; char phno[10]; }
struct book{ char isbn[]5; char author[7]; char title[25]; }

-ve: 1. Padding reqd to bring the fields upto a fixed length makes the file much larger. 2. May be data does not fit in allocated space. Solving problem 2 severes problem 1. Good when fileds are fixed in length or if there is very little variation in field length. File Structures

Record Organization

Record: a set of fields that belong together


Writing a memory resident record into the file: Saving state Reading a record from a file: Restoring the state of the object

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)

File Structures

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
John Alan 123 90 Maple Eastgate Stillwater Ada OK74075 OK74820

Ames Mason

(a)
Ames|John|123 Maple|Stillwater|OK|74075|

Unused space

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

Unused space

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

(c)

File Structures

Fig. 4.5

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 . . . (a)

Data file:

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

Index file:

00

40 . . . (b)

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

File Structures

Fig. 4.6

A Record Structure that uses a length Indicator

Before we begin writing, we must know the sum of lengths of fields in each record before we can begin writing the record to the file. Representing the record length field:

As a 2-byte binary integer:


No need to convert it into character form. Much bigger numbers can be represented (32767 versus 99) Stream.write(&length, sizeof(length) We cannot simply put it on screen. Stream << length

As a series of ASCII characters.

File Structures

10

A Record Structure that uses a length Indicator


Record length filed in character form 40 Ames|Mary|123 Maple|Stillwater|OK|74075| 36 Mason|Alan|90 Eastgate|Ada|OK|74820 Record length filed in binary form \0( Ames|Mary|123 Maple|Stillwater|OK|74075| \0$ Mason|Alan|90 Eastgate|Ada|OK|74820

File Structures

11

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

File Structures

12

Reading Variable Records


int ReadVariablePerson (istream & stream, Person & p) { // read a variable sized record from stream and store it in p short length; stream.read (&length, sizeof(length)); 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; }
File Structures 13

Readvar.cpp
int main() { char filename[20]; Person p; cout<< Enter the filename: <<flush; cin.getline(filename,19); ifstream stream (filename, ios::in); if (stream.fail()) { cout<< File open failed!<<endl; return 0; } while (1) { ReadVariablePerson(stream p); if(strlen(p.lastname) == 0) break; cout << p; } File Structures }

14

Read-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 2-byte integer
Decimal value of number Hex value stored in bytes ASCII character form
'4' '0'

(a) 40 stored as ASCII chars:

40

34 30

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

40

00 28

'\0'

"("

File Structures

15

ASCII Table
Character
null $ ( 0 4 A

Decimal
0 36 40 48 52 65

Octal
0 44 50 60 64 101

Hexadecimal
0 24 28 30 34 41

File Structures

16

Das könnte Ihnen auch gefallen