Chapter 13 Key Takeaways
Core Concepts
-
Files provide persistence. Without files, all program data is lost when the program ends. Files store data on disk, where it survives across sessions, reboots, and even hardware changes.
-
Pascal offers three file types, each with distinct strengths. - Text files (
TextFile): human-readable, line-oriented, sequential access. Best for logs, configuration, CSV, and data exchange. - Typed files (file of T): binary, fixed-record-size, sequential or random access. Best for database-like storage with records. - Untyped files (file): raw bytes, block-based I/O. Best for file copying, custom formats, and maximum performance. -
The file I/O workflow is always the same: declare, assign (
AssignFile), open (Reset/Rewrite/Append), read/write, close (CloseFile). Never skip the close step. -
Rewritedestroys existing files. It creates a new empty file, overwriting anything that was there before — silently and irreversibly. -
Typed file records must be fixed-size. Use
string[N](short strings), notstring(dynamic/AnsiString). A dynamic string is a pointer, and writing a pointer to a file is meaningless. -
Typed files support random access.
Seek(F, N)jumps to record N (0-based).FilePos(F)returns the current position.FileSize(F)returns the total record count. This makes typed files function like simple databases. -
After
Read, the file pointer advances. If you read record 5 and want to write back to position 5, you mustSeek(F, 5)again before writing. -
Error handling is mandatory for file I/O. Use
{$I-}to disable automatic checking, then callIOResultimmediately after the I/O operation. A return value of 0 means success; any other value indicates an error. -
IOResultmust be read immediately. Any intervening I/O operation — evenWriteLnto the screen — resets the result to 0. -
Use
try...finallyto guarantee file closure. Even if an exception occurs during processing, thefinallyblock ensuresCloseFileis called.
Decision Framework: Choosing a File Type
| If you need... | Use... |
|---|---|
| Human-readable output | Text file |
| Data exchange with other tools | Text file (CSV/TSV) |
| Fixed-record database storage | Typed file |
| Random access by record number | Typed file |
| File copying or raw binary work | Untyped file |
| Maximum I/O throughput | Untyped file |
Common Pitfalls
- Calling
Rewritewhen you meantAppend(destroys existing data) - Using dynamic strings in typed file records (writes pointers, not content)
- Forgetting to
Seekback afterReadwhen doing an update-in-place - Performing I/O between a
{$I-}operation and theIOResultcheck - Forgetting
CloseFile, which can cause buffered data to be lost - Not checking
Eofbefore reading, which causes read-past-end errors
Key Procedures and Functions
| Operation | Procedure/Function |
|---|---|
| Associate file variable | AssignFile(F, FileName) |
| Create/overwrite file | Rewrite(F) |
| Open existing file | Reset(F) |
| Open for appending (text) | Append(F) |
| Close file | CloseFile(F) |
| Move to record N | Seek(F, N) |
| Current record position | FilePos(F) |
| Total record count | FileSize(F) |
| Remove from current pos to end | Truncate(F) |
| End of file? | Eof(F) |
| End of line? (text only) | Eoln(F) |
| I/O error code | IOResult |
| File exists? | FileExists(Name) (SysUtils) |
| Delete file | Erase(F) or DeleteFile(Name) |
| Rename file | Rename(F, NewName) or RenameFile(Old, New) |
PennyWise Progress
With Chapter 13 complete, PennyWise now saves and loads expenses using typed files. Data persists between sessions. This transforms the project from a demonstration into a genuinely useful personal finance tool.