Skip to content

access methods

๐Ÿ”น 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 MethodFlexibilitySpeed (Random)Use CaseComplexity
SequentialLowSlowLogs, media filesLow
Direct (Random)HighFastDatabases, executablesMedium
IndexedVery HighVery FastLarge search systemsHigh

๐Ÿ”š 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.