| 1: || |
When is a sequential data file used?
| A1: || |
A sequential data file is used when the data can be written to the disk as one long sequential stream of data. A word processing document is a common example. You start at the beginning of the document and write the data letter by letter to the disk data file. The concept of a record is less often used with sequential data files, although random-length records in a sequential data file are possible.
| 2: || |
What does the term dense mean with respect to sequential data files?
| A2: || |
Sequential data files are usually written as a stream of consecutive bytes with no unused bytes in the file.
| 3: || |
What is one major advantage and one disadvantage of a sequential data files?
| A3: || |
The major advantage is that there are no gaps in the data; sequential files are dense. The major disadvantage is that sequential data files are difficult to search. To find a given piece of data, you must start at the beginning of the data file and read through it until you find the data you are looking for.
| 4: || |
What do BOF and EOF mean?
| A4: || |
These stand for beginning of file and end of file, respectively. Reading and writing disk data files use the BOF and EOF marks to position the file pointer. This file pointer positioning can be at either mark or as an offset from either mark.
| 5: || |
What are random access data files and how are they different from sequential files?
| A5: || |
The major difference between random access and sequential files is that random access files use fixed record lengths. Random access files are organized into records, and each record is allocated a fixed number of bytes. When you perform a read or write operation on a random access file, the operation is done in record-sized chunks of data. Therefore, if a random access record is defined using 100 bytes, the file is always read in 100-byte chunks . This makes searching random access files faster because indexes can be created that can be tied to the fixed position of a random access record. The major disadvantage is that the record size must be fixed to accommodate the largest- sized record, often resulting in a lot of wasted disk space.
| 6: || |
Explain briefly the purpose of the following two statements:
MyFile = New FileStream(mFileName, FileMode.Open, FileAccess.Write) MyFile.Position = (ThisRecord - 1) * mRecordSize
| A6: || |
The first statement is instantiating a FileStream object named MyFile and opening the file named mFileName for writing. The second statement is determining the byte offset where the new record is to be written. The variable mRecordSize is the size (in bytes) of each random access record. The variable ThisRecord is the record number to be written. The -1 is necessary because random access records are zero-based .
| 7: || |
Why are random access files written in binary mode?
| A7: || |
When data is written to disk files in a non-binary mode, there can be hidden data associated with each data item written to the disk. For example, writing string to a file writes a descriptor block that stores some overhead information associated with the string (for example, its length). These hidden pieces of information may change the fixed record length needed for random access files to work properly. By writing the data as raw binary data, you can avoid these hidden pieces of information resulting in the true fixed record length needed by random access files.
| 8: || |
What is the purpose of the following statement?
MyFile.Seek((ThisRecord - 1) * mRecordSize, SeekOrigin.Begin)
| A8: || |
The Seek() method is used to position the file pointer in a disk data file. The first argument is the product of the record we want to read ( ThisRecord “1) times the size of each record ( mRecordSize ). This product yields a number that is the byte offset to the record we're seeking. The second argument tells Visual Basic .NET the reference point for the offset. In our example, the reference point is BOF. You can also apply the byte offset to EOF or the current position of the file pointer.