6 Reading, writing, and formatting your data

 

This chapter covers:

  • Reading from keyboard and writing to screen
  • Standard input, output, error streams
  • Formatting numbers and text
  • Writing data to files on disk

One of the pillars of every useful program is its input and output (I/O). Almost every program reads some input data from keyboard, file, or network, does some calculation or processing on it, and outputs the result to the screen, file on disk, or some other device. If the program is designed to be used directly by a human user (rather than being a piece of some intricate pipeline), I/O becomes even more important. Specifically, inputting data to the program should be as easy as possible, with minimum effort for the user. The output should be easy to parse and not surprising. Some of the best end-user software out there is intuitive on the first try, without the need to refer to dense user manuals.

6.1  Your first I/O: Input from keyboard and output to screen

6.1.1  The simplest I/O

6.1.2  Reading and writing multiple variables at once

6.1.3  Standard input, output, and error

6.2  Formatting numbers and text

6.2.1  Designing the aircraft dashboard

6.2.2  Formatting strings, broken down

6.2.3  Format statements in legacy Fortran code

6.3  Writing to files on disk: A minimal note-taking app

6.3.1  Opening a file and writing to it

6.3.2  Opening a file

6.3.3  Writing to a file

6.3.4  Appending to a file

6.3.5  Opening files in read-only or write-only mode

6.3.6  Checking if a file exists

6.3.7  Error handling and closing the file

6.4  Answer Key

6.4.1  Exercise: Redirect stdout and stderr to files

6.5  New Fortran elements, at a glance

6.6  Summary

sitemap