This document discusses primary and secondary storage. Secondary storage is used for permanent storage of data in files and has greater storage capacity than primary storage. A file contains records with fields, and each record is uniquely identified by a key field like student ID. Logical files connect programs to physical files on secondary storage. Files can be accessed sequentially, randomly using indexing, or directly using the key value.
3. Secondary Storage Structure
Used for permanent storage
It’s a collection of records or a stream of bytes
Every Record is a collection of fields
A particular field is chosen as a Key
Records are organised in file by using the key. Primary
and secondary keys.
4. Consider a student database.
Every student has a unique record
Record has details of student-i.e name, Student
ID etc. These are the fields.
The unique key can be the Student ID. The
records can be organised in the file on basis of
student ID.
5. Sometimes data is too large to be stored in main
memory.
Maintaining permanent record is possible only by
using a secondary storage. Hence files.
6. Physical Files:
A collection of bits stored in the secondary storage
device
Logical File:
A channel that connects he program to the physical
file(Stream).
An example
FILE* out
out=fopen(“sample.txt”,”w”);
Here out is the logical file and sample.txt is the
physical file.
7. Opening a File:
A logical file is associated with the physical file
Closing a File:
The logical file associated with the physical file is freed.
fclose(file pointer);
Reading from file:
Data present in physical file is read by using the logical file
Writing to a File
Data can be written to physical file by using the logical file
8. Every logical file has a file position pointer.
When we open a new stream the position pointer
is set to beginning of the file.
As data is read or written the file position pointer is
moved accordingly.
9. To move file pointer to required position.
fseek(file pointer,offset, position);
To display current location of pointer.
long position=ftell(pointer);
To check for end of file
while(!feof(pointer));
10. Sequential File
Stored in the order entered
Random Access Files
An record is accessed using an index.(Hashing).
Direct Access Files:
The records are stored based on their relative
position with respect to first record.
Record with key 50 is placed at location 50
11. Records are stored in the order entered
Used when all the records have to be processed.
Complexity for searching O(n)
12. An record is accessed using an index.
The index of record position in file has to be
maintained in the main memory.
The Index can be created using hashing.
Search complexity is less. Complexity of Indexing
method used.
Disadvantage: While handling very large
databases its not possible to maintain an index in
the main memory.
13. The records are stored based on their relative
position with respect to first record.
Record with key 50 is placed at location 50
The search complexity is O(1)
Disadvantage is a lot of memory is wasted.
For example if no record has key 100 the position
100 is wasted.