Computer Science I / Input, Output, and File Thinking
Practice question · Multiple choice

The open-process-close discipline requires closing a file even when processing fails partway through. Why does that matter, when the program is ending anyway?

Hints
  1. Where does the data go between a write call and the disk?
  2. Ask what happens to a server that opens a file per request and never closes them.
Show the answer

D. Because buffered writes may be lost and file handles are a limited resource.

Why

Two distinct problems: writes sit in a buffer until it fills or the file closes, and handles are a finite per-process resource that a long-running program can exhaust. Data does go to disk progressively, so closing flushes what remains rather than writing everything. The failure path is easy to forget, which is why with, using, try-with-resources and RAII all exist.

Read the lesson: Input, Output, and File Thinking →

Practise Input, Output, and File Thinking

The app has 5 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.

More questions on Input, Output, and File Thinking