Beruflich Dokumente
Kultur Dokumente
File Structures
A Stream file
File structure ==> Persistency ==> Programs outlive the data in files
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
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
(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
-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
Writing a memory resident record into the file: Saving state Reading a record from a file: Restoring the state of the object
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
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
Data file:
Ames|John|123 Maple|Stillwater|OK|74075|Mason|Alan . . .
Index file:
00
40 . . . (b)
File Structures
Fig. 4.6
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:
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
File Structures
10
File Structures
11
File Structures
12
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
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'
40
34 30
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