2078 6

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]

Operating SystemsChapter 10: File System and Storage ManagementFile types and characteristics

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:

  1. Text Files - Store data as human-readable characters (ASCII/Unicode). Each line ends with a newline character. Example: .txt, .csv

  2. Binary Files - Store data in binary format (as bytes), not human-readable. Example: .exe, .jpg, .mp3

  3. Sequential Files - Records are stored and accessed one after another in order.

  4. Random Access Files (Direct Access Files) - Records can be accessed directly at any position without reading preceding records.

  5. 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:

OperationDescription
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:

FunctionDescription
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 file
  • SEEK_CUR - Current position
  • SEEK_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

FeatureSequential AccessRandom Access
Access methodOne by one in orderDirectly by position
SpeedSlow for specific recordFast for specific record
ComplexitySimpleSlightly complex
Use caseBatch processingDatabases, real-time
File pointerMoves only forwardCan move anywhere

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.