Pascal Programming/Input and Output

We already have been using I/O since the first chapter, but only to get going. It is time to dig a little bit deeper, so we can write nicer programs.

Interface
In its heydays Pascal was so smart and defined a minimal common, yet convenient interface to interact with I/O. Despite various standardization efforts I/O operations differ among every single OS, yet – as part of the language – Pascal defines a set of operations to be present, regardless of the utilized compiler or OS.

Special files
In the first chapter it was already mentioned that  and   are special program parameters. If you list them in the program parameter list, you can use these identifiers to write and read from the terminal, the CLI you are using.

Text files
In fact,  and   are variables. Their data type is. We call a variable that has the data type  a text file.

The data of a text file are composed of lines. A line is a (possibly empty) sequence of characters (e. g. letters, digits, spaces or punctuation) until and including a terminating “newline character”.

Files
A file – in general – has the following properties: All this information is implicitly available to you, you do not need to take care of it. You can query and alter some information in predefined ways.
 * It can be associated with an external entity. External means “outside” of your program. A suitable entity can be, for instance, your console window, a device such as your keyboard, or a file that resides in your file system.
 * If a file is associated with an external entity, it is considered bound.
 * A file has a mode. Every file can be in generation or inspection mode, none or both. If a file is in generation and inspection mode at the same time, this can also be called update mode.
 * Every file has a buffer. This buffer is a temporary storage for writing or reading data, so virtually another variable. This buffer variable exists due to reasons how I/O on computers works.

All you have to keep in mind in order to successfully use files is that a file has a mode. The text files  and   are, once they are listed in the program parameter list, in inspection and generation mode respectively. You can only  data from files that are inspection mode. And it is only possible to  data to files that are generation mode.

Note, due to their special nature the mode of  and   cannot be changed.

Routines
Pascal defines the following routines to read and write to files: The routines  and   can only be used in conjunction with text files, whereas all other routines work with any kind of file. In the following sections we will focus on  and. These routines build upon the “low-level”  and. In the chapter “Files” we will take a look at them, though.
 * /, and
 * /, and
 * /, and

Writing data
Let’s look at a simple program: Copy the program and see what it does.

Assignment
First, we will learn a new statement, the assignment. Colon equals is read as “becomes”. In the line  the variable’s value becomes ten. On the left hand side you write a variable name. On the right hand side you put a value. The value has to be valid for the variable’s data type. For instance, you could not assign  to the variable , because it is not a valid  , i. e. the data type  has.

Converting output
The power of /  is that – for text files – it converts the parameters into a human-readable form. On modern computers the  value ten is stored in a particular binary form. is a visual representation of the bits set and unset  for storing “ten”. Yet, despite the binary storage the characters you see on the screen are. This conversion, from zeroes and ones into a human-readable representation, the character sequence “10”, is done automatically.

Formatting output
Furthermore, after the parameter  comes. As you might have noticed, when you run the program the value ten is printed right-aligned making the  in   appear at the 20th column (position from the left margin).

The  is a format specifier. It ensures that the given parameter has a minimum width of that many characters and it may fill missing “width” with spaces to left.

Reading data
Look at this program:

Requirements
All parameters given to /  have to be variables. The first parameter, the source, has to be a file variable which is currently in inspection mode. We ensure that by putting  into the program parameter list. If the source parameter is, you are allowed to omit it, thus   is equivalent to.

Branching
A new language construct which we will cover in detail in the next chapter is the - -branch. The code after  that is surrounded by   and   is only executed if   equals to the character value. Otherwise, we are polite and do not express our strong disagreement.

Tasks
Notes: