BIT204 ยท 2078 ยท 5 marks
What are different types of file? Describe the sequential and random access of files.
What are different types of file? Describe the sequential and random access of files. [5]
Quick answer
Files can be classified based on their nature and access method: Text Files - Store data as human-readable characters (ASCII/Unicode); Binary Files - Store data in binary format (as bytes), not human-readable; Sequential Files - Records are stored and accessed one after another in order.
Types of Files, Sequential and Random Access
Types of Files
Files can be classified based on their nature and access method:
-
Text Files - Store data as human-readable characters (ASCII/Unicode). Each line ends with a newline character. Example:
.txt,.csv -
Binary Files - Store data in binary format (as bytes), not human-readable. Example:
.exe,.jpg,.mp3 -
Sequential Files - Records are stored and accessed one after another in order.
-
Random Access Files (Direct Access Files) - Records can be accessed directly at any position without reading preceding records.
-
Index Sequential Files - Combination of sequential and direct access; uses an index to locate records.
Sequential Access of Files
In sequential access, data is read or written in order from the beginning to the end of the file. To reach a particular record, all preceding records must be read first.
Characteristics:
- Simple to implement
- Suitable for batch processing (e.g., payroll, billing)
- Slow for searching a specific record
- Pointer moves only forward (one record at a time)
Operations:
| Operation | Description |
|---|---|
open() | Opens file at the beginning |
read() | Reads next record in sequence |
write() | Writes next record in sequence |
close() | Closes the file |
Example (in C-like pseudocode):
FILE *fp = fopen("data.txt", "r");
while (!feof(fp)) {
fscanf(fp, "%s", record);
// process record
}
fclose(fp);
Diagram:
[Record 1] --> [Record 2] --> [Record 3] --> [Record 4] --> EOF
Read 1st Read 2nd Read 3rd Read 4th
Random Access of Files
In random access (direct access), any record can be read or written directly by specifying its position (offset) in the file, without reading preceding records.
Characteristics:
- Faster access to specific records
- Suitable for databases and real-time systems
- Uses
seek()function to move file pointer to any position - Requires knowledge of record size for offset calculation
Key Functions:
| Function | Description |
|---|---|
fseek(fp, offset, origin) | Moves file pointer to a specific position |
ftell(fp) | Returns current position of file pointer |
rewind(fp) | Moves file pointer back to the beginning |
fseek() Origins:
SEEK_SET- Beginning of fileSEEK_CUR- Current positionSEEK_END- End of file
Example (in C):
FILE *fp = fopen("data.bin", "rb");
int recordSize = sizeof(struct Record);
int recordNo = 3; // Access 3rd record directly
fseek(fp, (recordNo - 1) * recordSize, SEEK_SET);
fread(&record, recordSize, 1, fp);
fclose(fp);
Diagram:
[Record 1] [Record 2] [Record 3] [Record 4] [Record 5]
^
|
Direct jump using fseek()
Comparison Table
| Feature | Sequential Access | Random Access |
|---|---|---|
| Access method | One by one in order | Directly by position |
| Speed | Slow for specific record | Fast for specific record |
| Complexity | Simple | Slightly complex |
| Use case | Batch processing | Databases, real-time |
| File pointer | Moves only forward | Can move anywhere |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.