Published by Patrick Mutisya · 14 days ago
In computer systems, a file is a named collection of related data stored on secondary storage. Files provide a mechanism for preserving information beyond the lifetime of a program’s execution and for organising data in a way that can be accessed by multiple programs or users.
| Aspect | Variables (RAM) | Files (Secondary Storage) |
|---|---|---|
| Lifetime | Temporary – lost when program ends | Persistent – survives power‑off |
| Capacity | Limited by available RAM | Limited by disk space (typically much larger) |
| Accessibility | Accessible only to the running program | Accessible to many programs and users |
| Speed of Access | Fast (nanoseconds) | Slower (micro‑ to milliseconds) |
| Typical Use | Intermediate calculations, temporary storage | Long‑term storage, data exchange, logging |
Consider a student‑record system that must retain information about each pupil for several years. The program can:
students.txt at start‑up to load existing records into memory.students.txt before exiting, ensuring the changes are saved for future sessions.This workflow demonstrates persistence, data sharing (multiple users can open the same file), and the ability to handle a data set that may exceed the available RAM.
If a program processes \$n\$ data items, each of size \$s\$ bytes, the total memory required is \$M = n \times s\$. When \$M\$ exceeds the available RAM \$R\$, the program must use external storage:
\$\$
\text{If } n \times s > R \;\Longrightarrow\; \text{store data in a file.}
\$\$