๐น 1. Sequential Access
๐ Definition:
Accesses data in a file in order, one record after another, from beginning to end.
๐ How It Works:
- Reading starts at the beginning.
- Read or write moves the file pointer forward.
- No skipping or jumping between data blocks.
๐ Use Cases:
- Text editors
- Log files
- Audio/video streaming
โ Advantages:
- Simple to implement.
- Efficient for processing large volumes of data.
โ Disadvantages:
- Inefficient for searching or updating specific records.
๐งช Example:
while (fgets(buffer, sizeof(buffer), file) != NULL) {
printf("%s", buffer);
}
๐น 2. Direct Access (Random Access)
๐ Definition:
Also called relative access, this method allows jumping to any location in the file to read or write data.
๐ How It Works:
- Uses byte offset or record number to access data.
- File is treated like an array of fixed-size blocks.
๐ Use Cases:
- Databases
- Compilers
- Large binary files (images, executable files)
โ Advantages:
- Fast access to any part of the file.
- Ideal for read-heavy operations.
โ Disadvantages:
- More complex to implement.
- Requires known structure (e.g., fixed record size).
๐งช Example:
fseek(file, 100, SEEK_SET); // Jump to byte 100
fread(buffer, sizeof(char), 20, file); // Read 20 bytes
๐น 3. Indexed Access
๐ Definition:
Accesses records using an indexโa data structure that maps keys to file locations (like a book index).
๐ How It Works:
- An index is built containing key and pointer pairs.
- The pointer tells where in the file the record is located.
๐ Use Cases:
- Large database systems
- Key-value storage systems (e.g., NoSQL)
- Library catalogues
โ Advantages:
- Fast search and retrieval using keys.
- Supports both sequential and random access.
โ Disadvantages:
- Index maintenance overhead.
- Extra storage required for the index.
๐งช Conceptual Example:
Index Table:
Key | File Offset
--------------------
1001 | 0
1002 | 128
1003 | 256
To retrieve key 1002 โ Go directly to byte 128 in the file.
๐ Comparison Table:
Access Method | Flexibility | Speed (Random) | Use Case | Complexity |
---|---|---|---|---|
Sequential | Low | Slow | Logs, media files | Low |
Direct (Random) | High | Fast | Databases, executables | Medium |
Indexed | Very High | Very Fast | Large search systems | High |
๐ Conclusion:
- The Operating System may support one or more access methods depending on file type and application needs.
- Indexed access is the most powerful but also the most resource-intensive.
- Sequential access remains popular for simplicity and stream processing.