# Input, Output, and File Thinking

Computer Science I · Programming I · https://tryals.app/learn/computer-science-i/input-output-and-file-thinking

## Getting Data In and Out

A program that cannot communicate is useless. **Input** brings data in from the keyboard, files, or networks, while **output** sends results out to the screen, files, or devices.

Input usually arrives as a **string**, even when it looks like a number. Reading "42" from the keyboard gives the string "42", not the integer `42`.

To do arithmetic, you must **convert** (parse) it using $int("42") \rightarrow 42$. Forgetting this is why input "1" + "1" produces "11" instead of `2`.

| Input Source | Returned Type | Action Required |
| --- | --- | --- |
| Keyboard | **String** | Parse with $int()$ or $float()$ |
| File | **String** | Parse before math operations |

## File Thinking and Streams

**Files** store data that outlasts a single run. The standard file discipline requires three steps: **open** the file, **process** it, and **close** it to flush writes.

| Step | Purpose |
| --- | --- |
| **Open** | Connect to the file in read or write mode |
| **Process** | Read from or write data to it |
| **Close** | Release the resource and flush writes |

A **stream** reads a file piece by piece (like line by line) rather than loading it entirely into memory. This handles files far larger than available RAM.

> **Common Pitfall:** Treating input as already numeric. Always convert strings _before_ math, and expect failures if the text isn't a valid number.

## Practice questions

6 of this lesson's 11 practice questions, with answers. The full set is in the app.

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

A. Because an unclosed file cannot be read by any other program ever again.
B. Because closing is what actually writes the data, so nothing is saved until it happens.
C. Because the operating system charges the program for each second a file stays open.
D. Because buffered writes may be lost and file handles are a limited resource.

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

Page: https://tryals.app/practice/computer-science-i/input-output-and-file-thinking/the-open-process-close-discipline-requires-closing-a-file-even-when

### 2. Match each I/O idea to what it means.

**Answer:**

- Input → Data brought into the program
- Output → Results sent out of the program
- Parsing → Converting text into a number
- Stream → Reading a file piece by piece rather than all at once

**Why:** Input and output are the two directions; parsing turns text into a usable number; a stream processes a file incrementally so it can be far larger than available memory. The stream model is why a program can read a gigabyte file on a machine with less free memory than that.

Page: https://tryals.app/practice/computer-science-i/input-output-and-file-thinking/match-each-i-o-idea-to-what-it-means

### 3. A program writes to a file, crashes, and the file turns out empty, although the writes clearly executed. Where did the data go?

A. The crash corrupted the file system, erasing the file's contents
B. The writes were rejected because the file was still open
C. It was still in a buffer, flushed only on close or when the buffer fills
D. The operating system rolled the file back to its previous state

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

Page: https://tryals.app/practice/computer-science-i/input-output-and-file-thinking/a-program-writes-to-a-file-crashes-and-the-file-turns-out-empty

### 4. Sort each action by whether it is input or output.

**Answer:**

- Input: Reading a line the user typed, Reading numbers from a file, Receiving a message over the network
- Output: Printing a result to the screen, Writing a report to a file, Sending a message over the network

**Why:** Reading and receiving bring data in; printing, writing and sending push data out. The medium does not decide the direction, a file or network can be read (input) or written (output), so the verb is what matters.

Page: https://tryals.app/practice/computer-science-i/input-output-and-file-thinking/sort-each-action-by-whether-it-is-input-or-output

### 5. Select every statement that is true of the stream model and file handling.

A. A file must always be loaded entirely into memory before use
B. A stream reads a file a piece at a time
C. Opening a file requires choosing a read or write mode
D. Closing a file flushes writes that were still buffered
E. Streaming lets a program handle a file larger than available memory

**Answer:** B. A stream reads a file a piece at a time; C. Opening a file requires choosing a read or write mode; D. Closing a file flushes writes that were still buffered; E. Streaming lets a program handle a file larger than available memory

**Why:** Streaming reads incrementally, which is exactly what lets a program exceed memory; closing flushes buffered writes; opening picks a mode. The false option is that a file must be fully loaded first, that is what the stream model is designed to avoid.

Page: https://tryals.app/practice/computer-science-i/input-output-and-file-thinking/select-every-statement-that-is-true-of-the-stream-model-and-file

### 6. Order the steps a program takes to read a number from the user and add ten to it.

**Answer:**

1. Prompt the user to type a number
2. Read the input, which arrives as a string
3. Convert the string to an integer with int(...)
4. Add ten to the integer
5. Print the result

**Why:** Prompt, read text, convert to a number, then compute, then print. Doing the arithmetic before the conversion is the classic failure, it either joins strings or raises an error, because the input is text until int(...) turns it into a number.

Page: https://tryals.app/practice/computer-science-i/input-output-and-file-thinking/order-the-steps-a-program-takes-to-read-a-number-from-the-user-and
