Lesson 10 of 15

File Allocation Table

The File Allocation Table

FAT (File Allocation Table) is a simple filesystem used on USB drives and SD cards. It stores a linked list of disk blocks for each file.

The FAT is an array indexed by block number. Each entry is either:

  • the next block in the file's chain
  • -1 (end of file — EOF)
  • 0 (free block)
block:  0  1  2  3  4  5  6  7
fat:   -1  3  0  5  0  7  0 -1
             ↑
         block 1 → 3 → 5 → 7 → EOF

To read a file starting at block 1: follow the chain 1 → 3 → 5 → 7 → EOF.

Your Implementation

Write void fat_chain(int fat[], int start) that follows the chain from start and prints each block number until EOF (-1):

void fat_chain(int fat[], int start) {
    int cur = start;
    while (cur != -1) {
        printf("%d\n", cur);
        cur = fat[cur];
    }
}

Your Task

Implement fat_chain that traverses the FAT linked list from start and prints each block number.

TCC compiler loading...
Loading...
Click "Run" to execute your code.