Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lec2 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

File Structure

CS 316

Methods of field/record
organization
Chapter 4
LEC2

Reham Adel

1
Contents

► Methods of field organization

► Methods of record organization

► CPP code examples


File types
A file can be treated as
◦ a stream of bytes , ex: image file , sound
files
◦ a collection of records with fields ..
Sequential access vs Direct Access
Sequential Search
– Look at records sequentially until matching
record is found.
– Time is in O(n) for n records.
– Appropriate for Pattern matching, file with few
records
Direct Access
– Being able to seek directly to the beginning of the
record.
– Time is in O(1) for n records.
– Possible when we know the Relative Record
Number (RRN):
– First record has RRN 0, the next has RRN 1, etc.
Direct access using RRN
Requires records of fixed length.
– RRN=30 (31st record)
– Record length = 101 bytes
– Byte offset = 30 × 101 = 3030

►Now, how to go directly to the byte


3030 in the file
– By seeking  f.seekg(3030, ios::beg)
Methods of field organization
 Field: a data value, smallest unit of data with logical
meaning

► Record: A group of fields that forms a logical unit

►Key: a subset of the fields in a record used to


uniquely identify the record
Field
key Record
87358CARROLL ALICE IN WONDERLAND
03818FOLK FILE STRUCTURES Each line of the file
is a record.
79733KNUTH THE ART OF COMPUTER
►Fields in each
record:
PROGRAMMING – ISBN Number,
86683KNUTH SURREAL NUMBERS– Author Name,
– Book Title
18395TOLKIEN THE HOBITT
Method of field organization
1. Fixed length
2. Begin each field with its Length
indicator
3. Delimiters to separate fields
4. “keyword=value” identifies each field
and its content
Methods of field organization
1- Fixed length fields
 Like in our file of books (field lengths are 5,7, and 25

 87358CARROLL ALICE IN WONDERLAND----


 03818FOLK - - - FILE STRUCTURES--------------
 86683KNUTH- - SURREAL NUMBERS------------
 18395TOLKIEN THE HOBITT------------------------
Methods of field organization
2-Length indicator
Filed
length Record
 058735807CARROLL19ALICE IN
WONDERLAND
 050381804FOLK15FILE STRUCTURES
 058668305KNUTH15SURREAL
NUMBERS
 051839507TOLKIEN10THE HOBITT
Methods of field organization
3-using delimiters
 Add delimiter at the end of each
field

 87358|CARROLL|ALICE IN
WONDERLAND|
 03818|FOLK|FILE STRUCTURES|
 86683|KNUTH|SURREAL
NUMBERS|
 18395|TOLKIEN|THE HOBITT|
Methods of field organization
4-using (key=value)

 ISBN=87358|AU=CARROLL|TI=ALICE
IN WONDERLAND|
 ISBN=03818|AU=FOLK|TI=FILE
STRUCTURES|
 ISBN=86683|AU=KNUTH|TI=SURREA
L NUMBERS|
 ISBN=18395|AU=TOLKIEN|TI=THE
HOBITT|
Field structures: advantages and
disadvantages
Type Advantages Disadvantages

Fixed Easy to Waste space with


Read/Store padding

Width length Easy to jump Long fields require


indicator ahead to more
the end of the than 1 byte to store
field length
(Max is 255)
Field structures: advantages and
disadvantages
Type Advantages Disadvanta
ges
Delimited Fields May waste less Have to check
space every byte
than with length- of field against
based the
delimiter
Keyword Fields are self Waste space with
describing allows keywords
for
missing fields
Methods of record organization
Fixed
Record Organization length
record
1. Fixed length record
2. Records with fixed number of fields
3. Begin record field with its Length
indicator
4. Using Delimiters to separate records
5. Use an index to keep records addresses
Variable
length record
Record Organization
1-Fixed length record
 Internal organization using
◦ Fixed length field

◦ Delimited fields

◦ Using length indicator

◦ Note:
◦ Deleting any record will create an empty block which may occur
waste of memory space.
Record Organization
1-Fixed length record
• So to overcome this problem we must fill this block with
records of the file, or we must have way of marking
deleted records so that it can be ignored.
Record Organization
1-Fixed length record
Record Organization
2-Fixed number of fields
 Internal organization may be
◦ Delimited fields

◦ Using length indicator


Record Organization
3-Begin record with its Length
indicator
 Internal organization may be
◦ Delimited fields

◦ Using length indicator

33
Record Organization
4- Using Delimiters to separate
records

• The common delimiter in the end of line,


as it make the file readable

*any other character can be used as a delimiter.


Record Organization
5-using index to keep track of
records.

Keys are
sorted
Fixed length vs. variable length
records
CPP code examples
Simple file
Output to File test.txt
----------------------------------------------------
---- This is how the
file will look
Jones
like
Smith
Willis
Davis
Int eof();
-It returns non-zero when the end of file is
reached otherwise, it returns zero.
Opening files

Openin
(modes
Closing file
Closing file (Cont.)
File seek operation
 A fstream has 2 file pointers:
◦ get pointer (for input)
◦ put pointer (for output)
 file1.seekg ( byte_offset, origin); //moves
get pointer
 file1.seekp ( byte_offset, origin); //moves
put pointer
ios::beg (beginning of
file)
origin can be ios::cur (current
position)
ios::end (end of file)
File seek operation
How it Affects the
Statement
Read/Write Position
Sets the write position to the 33rd byte (byte 32) from the
File.seekp(32L, ios::beg);
beginning of the file.
Sets the write position to the 11th byte (byte 10) from the end
file.seekp(-10L, ios::end);
of the file.
Sets the write position to the 121st byte (byte 120) from the
file.seekp(120L, ios::cur);
current position.
Sets the read position to the 3rd byte (byte 2) from the
file.seekg(2L, ios::beg);
beginning of the file.
Sets the read position to the 101st byte (byte 100) from the end
file.seekg(-100L, ios::end);
of the file.
Sets the read position to the 41st byte (byte 40) from the
file.seekg(40L, ios::cur);
current position.
file.seekg(0L, ios::end); Sets the read position to the end of the file.
Example
#include <iostream.h>
#include <fstream.h>
void main(void)
{ File contents:
fstream file(“d:\\letters.txt",
ios::in); abcdefghijk
char ch; f
file.seekg(5L, ios::beg);
file.get(ch);
cout << "Byte 5 from beginning: " <<
ch << endl; g
file.seekg(-5L, ios::end);
file.get(ch);
cout << "Byte 10 from end: " << ch k
<< endl;

file.seekg(3L, ios::cur);
file.get(ch);
cout << "Byte 3 from current: " <<
ch << endl;
The tellp and tellg Member
Functions
 tellp returns a long integer that is the
current byte number of the file’s write
position.

 tellg returns a long integer that is the


current byte number of the file’s read
position.
Example1
write a C++ program, with no loops, to copy the
contents of the file d:\\source.txt to the file d:\\destination.txt,
assume that destination file is empty, don’t use any loops.
int main ()
{
ifstream infile ("source.txt",ios::binary);
ofstream outfile
("destination.txt",ios::binary);
infile.seekg (0,infile.end);
long size = infile.tellg();

infile.seekg (0);//by default seeks from


beginning
char* buffer = new char[size];
infile.read (buffer,size);
outfile.write (buffer,size);

delete[] buffer; outfile.close();


Cin vs. cin.get() vs. cin.getline()
#include <iostream>
using namespace std; Output
int main() --------------------
{
char ch;
Enter a number: 100
int number; 100
Enter a character: A
cout << "Enter a number: "; A
cin >> number;
cout << number;
Thank You!
cin.ignore(); // Skip the newline
character
cout << “\nEnter a character: ";
ch = cin.get();
cout << ch;

cout << “\nThank You!\n";


system("pause");
return 0;
}
getline Example cont.

cin.clear(); //clear the eof flag setted by ctrl+Z

char data[100];
cin.getline(data,100, '#');
cout<<data; will continue
reading till
find ‘#’, or
reach 100
} char, \n will
be read as a
regular char.

You might also like