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
- Where does the data go between a write call and the disk?
- 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.
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
- Match each I/O idea to what it means.
- A program writes to a file, crashes, and the file turns out empty, although the writes clearly executed.…
- Sort each action by whether it is input or output.
- Select every statement that is true of the stream model and file handling.
- Order the steps a program takes to read a number from the user and add ten to it.