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[edit | edit source]
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[edit | edit source]
In the first chapter it was already mentioned that
output 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[edit | edit source]
output are variables.
Their data type is
We call a variable that has the data type
text 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[edit | edit source]
A file – in general – has the following properties:
- 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.[fn 1]
- 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 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.
All you have to keep in mind in order to successfully use files is that a file has a mode.
The text files
output are, once they are listed in the program parameter list, in inspection and generation mode respectively.
You can only
read data from files that are inspection mode.
And it is only possible to
write data to files that are generation mode.
Note, due to their special nature the mode of
output cannot be changed.
Routines[edit | edit source]
Pascal defines the following routines to read and write to files:
writeLn 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
These routines build upon the “low-level”
In the chapter “Files” we will take a look at them, though.
Writing data[edit | edit source]
Let’s look at a simple program:
program writeDemo(output); var x: integer; begin x := 10; writeLn(output, x:20); end.
Copy the program and see what it does.
Assignment[edit | edit source]
First, we will learn a new statement, the assignment.
Colon equals (
:=) is read as “becomes”.
In the line
x := 10 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
'Hello world!' to the variable
x, because it is not a valid
integer, i. e. the data type
Converting output[edit | edit source]
The power of
writeLn is that – for text files – it converts the parameters into a human-readable form.
On modern computers the
integer value ten is stored in a particular binary form.
00001010 is a visual representation of the bits set (
1) and unset (
0) 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.
|If the destination of |
Formatting output[edit | edit source]
Furthermore, after the parameter
As you might have noticed, when you run the program the value ten is printed right-aligned making the
10 appear at the 20th column (position from the left margin).
:20 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.
|Format specifiers in a |
Reading data[edit | edit source]
Look at this program:
program iceCream(input, output); var response: char; begin writeLn('Do you like ice cream?'); writeLn('Type “y” for “yes” (“Yummy!”) and “n” for “no”.'); writeLn('Confirm your selection by hitting Enter.'); readLn(input, response); if response = 'y' then begin writeLn('Awesome!'); end; end.
Requirements[edit | edit source]
All parameters given to
readLn 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
input into the program parameter list.
If the source parameter is
input, you are allowed to omit it, thus
readLn(response) is equivalent to
Branching[edit | edit source]
A new language construct which we will cover in detail in the next chapter is the
The code after
then that is surrounded by
end; is only executed if
response equals to the character value
Otherwise, we are polite and do not express our strong disagreement.
Tasks[edit | edit source]
- “Update” mode is only available in Extended Pascal (ISO standard 10206). In Standard (unextended) Pascal (laid out in ISO standard 7185) a file can be either in inspection or generation mode, or none.