Chapter 13 Key Takeaways

Core Concepts

  1. 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.

  2. 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.

  3. 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.

  4. Rewrite destroys existing files. It creates a new empty file, overwriting anything that was there before — silently and irreversibly.

  5. Typed file records must be fixed-size. Use string[N] (short strings), not string (dynamic/AnsiString). A dynamic string is a pointer, and writing a pointer to a file is meaningless.

  6. 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.

  7. After Read, the file pointer advances. If you read record 5 and want to write back to position 5, you must Seek(F, 5) again before writing.

  8. Error handling is mandatory for file I/O. Use {$I-} to disable automatic checking, then call IOResult immediately after the I/O operation. A return value of 0 means success; any other value indicates an error.

  9. IOResult must be read immediately. Any intervening I/O operation — even WriteLn to the screen — resets the result to 0.

  10. Use try...finally to guarantee file closure. Even if an exception occurs during processing, the finally block ensures CloseFile is 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 Rewrite when you meant Append (destroys existing data)
  • Using dynamic strings in typed file records (writes pointers, not content)
  • Forgetting to Seek back after Read when doing an update-in-place
  • Performing I/O between a {$I-} operation and the IOResult check
  • Forgetting CloseFile, which can cause buffered data to be lost
  • Not checking Eof before 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.