Practice question · Multiple choice
A program writes to a file, crashes, and the file turns out empty, although the writes clearly executed. Where did the data go?
Hints
- Ask where the bytes sit between the write call returning and the disk spinning.
- Why does closing a file matter if the writes already 'succeeded'?
Show the answer
C. It was still in a buffer, flushed only on close or when the buffer fills
Why
Buffering makes writes fast by batching them, so a successful write call means the data reached a buffer rather than the disk. Closing flushes it, which is why with blocks, RAII and try-with-resources exist, and why a crash is precisely the case that skips a manual close.
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
- The open-process-close discipline requires closing a file even when processing fails partway through. Why…
- Match each I/O idea to what it means.
- 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.