Lec2 PDF
Lec2 PDF
Lec2 PDF
CS 316
Methods of field/record
organization
Chapter 4
LEC2
Reham Adel
1
Contents
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
◦ Delimited fields
◦ 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
33
Record Organization
4- Using Delimiters to separate
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.
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.