Diploma in Nursing Admission Test Question Solution 2023.pdf
CS215 - Lec 7 managing records collection
2. Storing collection of records.
Reading collection of records.
Writing collection of records.
Buffering Algorithm.
Dr. Hussien M.
Sharaf
2
3. Company info class only
represents single record.
A collection of records
needs to be stored into a
dynamic array.
The collection of records
can be encapsulated into a
class that maps to a file.
Dr. Hussien M.
Sharaf
3
Company1
Company2
…..
Company N
5. Dr. Hussien M.
Sharaf
5
CompanyInfoCollection
Functionalities
Record 3 Record 4Record 1 Record 2210 250 180 190
1. Responsible for opening and closing of the file
stream.
2. Calling read or write of companyInfo without
knowing the details of read/write single line.
3. Manage the buffering of records while reading or
writing.
4. Count the number of records in a file.
6. Dr. Hussien M.
Sharaf
6
Algorithm#1 Writing collection of
records
Record 3 Record 4Record 1 Record 2210 250 180 190
While (len(Buffer) < bufferingSize)
{
companyInfo[i].writeIntoStream( stringStream)
stringStream<<recordDelimiter
i++
}
Stringstream>>Buffer
FileStream.write(Buffer)
7. Dr. Hussien M.
Sharaf
7
Algorithm#2 Reading collection
of records
Record 3 Record 4Record 1 Record 2210 250 180 190
FileStream.read(Buffer, BufferingSize+1)
Stringstream<<Buffer;
While (! Stringstream.EOF)
{
companyInfo[i].ReadFromStream(Stringstream)
i++
}
8. 1. Read x MB into memory buffer (i.e.
char array).
2. Make sure to stop reading at the end of
a record. How can this be done?
3. Put the buffered data into a string
stream.
Dr. Hussien M.
Sharaf
8
9. 4. Pass the string stream to an instance of
companyInfo which extracts fields.
5. Add the new companyInfo to the array
of records.
6. Manage the index by adding an entry
with the correct offset.
7. Repeat steps 1-6 until EOF
Dr. Hussien M.
Sharaf
9
11. Store the byte offset of each record into a list stored in
another file.
Dr. Hussien M.
Sharaf
11
Remember Indexing
Problems:
1. Two files are used: one for the data and another for
CompanyName offset
IBM 0Record1
n
n
Google 211Record2 n
Microsoft 462Record3 n
ITE 643Record4 n
12. Dr. Hussien M.
Sharaf
12
class CompanyInfoCollection
{
private:
vector<CompanyInfo> Records;
RecordIndex recordIndexes;
public:
//read delimited fields
bool WriteDelimRecords(ostream& ,char);
//read delimited fields
bool readDelimRecords(istream&);
};
13. Dr. Hussien M.
Sharaf
13
Read/write from
streamBuffer
Single
companyInfo
RecordsCollection
File stream
Read/write from
streamBuffer
Single
companyInfo
Records Indexes
Record 2211 Record 3462
14. Continue using the CompanyInfo program:
1) Where would you put the following
functionalities:
a. Read the multiple records into a buffer.
b. Split a single record into fields.
c. Append a single record into a collection of
records.
d. Search for a record given a company
name.
Dr. Hussien M.
Sharaf
14
15. Continue using the CompanyInfo program:
2) Implement Algorithm#1 into the
CompanyInfo program.
3) Implement Algorithm#2 into the
CompanyInfo program.
Dr. Hussien M.
Sharaf
15