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 . Forgetting this is why input "1" + "1" produces "11" instead of 2.
| Input Source | Returned Type | Action Required |
|---|---|---|
| Keyboard | String | Parse with or |
| 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.