Title: Programmers Guide to F
1Programmers Guide to F
- Chapter 9
- Formatted Input Output
- Version 2.0
- Last Update May 15, 2006
2Simple INPUT and OUTPUT (I/O)
- The input and output facilities of any
programming language are extremely important,
because communication between the user and the
program is carried out through these features. - F provides facilities for input and output at two
quite different levels. - The so-called list-directed input and output
statements that we have been using so far
provide the capability for simple input from the
keyboard and simple output to the screen or
printer. Eg - integer n
- print , Enter a positive integer n !
Prints on the screen. - read , n ! Reads from the
keyboard - These list-directed I/O - statements, allow the
user very little control over the source or
layout of the input data.
3More Control overINPUT and OUTPUT (I/O)
- F allows the programmer to specify
- from which of the available input units the data
will be read - in which format the data will be presented at the
input unit - to which of the available output units the
results will be sent - in which format the results are to be displayed.
4(I/O) Statements (Summary)
- input
- read (ltcontrol_listgt) ltlistgt
- read (unit 1, fmt (i4,f3.2)) a, b
- output
- write (ltcontrol_listgt) ltlistgt
- write (unit 6, fmt (i4,f3.2)) a, b
- print (ltformat_specifiergt) ltlistgt
- print , a, b
5I/O operations in F
- read
- print
- write
- open
- close
- inquire
- backspace
- endfile
- rewind
actual data transfer
connection between an I/O unit and a file
find out things about an I/O unit or a file
affect the position of the file
I/O operations deal with files. A file is a
collection of data records, which will be
discussed next.
6Records
- A data record is a sequence of values.
- These values may be represented either formatted
or unformatted. -
- F may read and write three types of data records
- (a) formatted, (b) unformatted,
(c) end-file records - A formatted record contains only formatted data.
- A formatted record is composed of characters
only. - These characters include letters, digits, and
special symbols from the ASCII character set -
- The statement
- write(, (i1,a,i2)) 6,,,11
- would produce a record, which can be
schematically represented as -
-
7Records
- Unformatted data records consist of values
represented just as they are stored in computer
memory. - The structure of unformatted records is
processor-dependent, since the amount of space
required on the external unit depends on the
details of the data representation on that
processor. - If integers are stored using an eight bit binary
representation, - write(9) 6, 11
- would produce an unformatted record such as
- Unformatted records contain only unformatted data.
8endfile
The third kind of data records is the endfile
record, which, at least conceptually, has no
values and has no length. There can be only one
endfile record in a file and, it must be the last
record of a file. An endfile record may be
written explicitly by the programmer using the
endfile statement.
9 - Consider the following 9-digit input data
- 1 2 3 4 5 6 7 8 9
- There are an enormous number of possible
- interpretations
- 1 2 3 4 5 6 7 8 9
- 1, 2, 3, 4, 5, 6, 7, 8, and 9
- 123, 456, 789
- 12345 . 6789
- 1.23, .45, 67, 8900
10FORMAT and EDIT DESCRIPTORS
- An input (read) statement must contain three
distinct - types of information
- where the data is currently residing (sitting).
- how it is to be interpreted.
- where it will be stored in the computers memory
after it is read by the computer. - Similarly, an output (print or write) statement
must define - where the results are currently stored in the
computers memory - where they are to be sent
- in what form they are to be displayed.
11INPUT Editing for Integers
Simplest edit descriptor is for whole numbers,
which are to be stored in an integer
variable iw If you wished to read the
line 123456789 as a single integer to be stored
in integer variable n read (i9), n as three
separate integers (123, 456, 789) then read
(i3,i3,i3), n1, n2, n3
12List of input edit descriptors
Descriptor
Meaning iw Read the next w characters as
integers. fw.d Read the next w characters as
a real number with d digits after the
decimal point. aw Read the next
w characters as characters. Lw
Read the next w characters as a logical
value. tc Next character to be
read is at position c counting from the very
left. tln Next character to be read
is n characters before the current
position. trn Next character to be read is n
characters after the current position.
13INPUT Editing for Real Numbers
- The next edit descriptor is for real numbers,
which - are to be stored in a real variable
- fw.d
- w characters are read as real numbers, and d
- shows decimal point. Your character is again
- 123456789
- read (f9.4), real_n
- will cause the first 9 characters to be read as
- 12345.6789
14Example
- Example for formatting of real numbers using the
fedit descriptor depending on two different
INPUT DATA sets - read (f3.1, f2.2, f3.0), s1,s2,s3
- If the data to be read contains a decimal point
like in DATA 2 the second part of the edit
descriptor is ignored. - DATA 1 DATA 2
- 123456789 .23.56.89
- s1 contains 12.3 0.23
- s2 contains 0.45 0.5
- s3 contains 678.0 6.8
15The exponential format using e, E, d, D
- A real number can be written in many different
- ways, e.g. 361.764 can be written in all of the
following - forms
- 361.764
- 3.61764e2
- 0.361764d3
- 0.0361764e4
- 3617.64d-1
- 3.61764E2
- 361.764D0
16INPUT Editing for Character Data
- The third major data edit descriptor is the a
edit descriptor which is used to control the
editing of character data - aw
- If w lt len extra blank characters will be added
at the end - If w gt len the rightmost len characters of the
input character string will be stored
17Example
- Example for formatting of an input character type
using the - aedit descriptor .
- character ( len 10 ) ch1
- character ( len 6 ) ch2
- character ( len 15 ) ch3
- When the above declarations are used, the
following two - statements have the same effect
- read (a10, a6, a15) , ch1, ch2, ch3
- read (a, a, a), ch1, ch2, ch3
18Logical data - input editing
- The edit descriptor which is used with logical
- data takes the following form Lw
- Note that the upper case L is used instead of
l to avoid potential confusion with the digit
1. - w characters are used to derive either a
- .true. value, or a .false. value or an error.
- The input field for a logical value consists of
any number of blanks, followed by an optional
period, followed by t, or T, or f, or F, followed
by anything.
19 Logical data - input editing
- Any of the following are acceptable as
representing .true. - t
- true
- .T.
- .true.
- .trabzon
- Turkiye
- while the following will all be interpreted as
false - F
- False
- .f.
- .false.
- .fadil.
- FAZIL
20OUTPUT Editing
- The edit descriptors used for output are same as
those - used for input.
- However, there are some additional ones for
output.
21List of output edit descriptors
- Descriptor Meaning
- iw Output an integer in the next
w characters. - fw.d Output a real number in the next
- w characters with
d decimal places. - aw Output a character string in
the next w characters. - a Output a character string with
no trailing blanks. - Lw Output w-1 blanks, followed by T or
F as logical value. - tc Output the next item starting at position c
starting from - the very left.
- tln Output the next item starting n
character positions before - the current
position. - trn Output the next item starting n character
positions after - the current
position.
22Tn causes subsequent output to start at column
n.Trn causes a shift to the right by n
columns.Tln causes a shift to the left by n
columns (however, it will not move the
position to the left of column 1).
- total24
- ave2.43
- dev0.32
- write "(t3,a4,i5,t15,a8,f4.2,t30,a10,f4.2)",
- sum",total,average",ave, deviation,dev
- is the same as
- write "(t3,a4,i5,tr4,a8,f4.2,tr4,a10,f4.2)",
- sum",total,average",ave, deviation,dev
- The print out will be as follows
- _ _ sum_ _ _ 24_ _ _ average2.43_ _ _
deviation0.32
23OUTPUT EDITING i-edit descriptorfor integers
- The i edit descriptor (iw) causes an integer to
be output using - w character positions
- Example
- tom 23
- dick 715
- harry -12
- write (i5,i5,i5) , tom, dick, harry
- This statement will produce the following line of
output - (where the symbol represents a space )
- 23715-12
24OUTPUT EDITING f-edit descriptor for real numbers
- The f edit descriptor (fw.d) causes a real number
to be - output using w characters with d digits after the
decimal - point.
- Example
- x -3.14159
- y 8275.3024
- z 12.9999
- write (f10.3,f10.3,f10.3) , x, y, z
- This statement will produce the following line of
output - (where the symbol represents a space )
- -3.1428275.30213.000
- Rounded up Rounded down Rounded up
25Example Program
- program tabular_output
- real, parameter third 1.0 / 3.0
- real x
- integer i
- do i 1, 10
- x i
- write (f15.4, f15.4,f15.4), x,
sqrt(x), xthird - !write (3f15.4), x, sqrt(x), xthird
- !would do the same job.
- end do
- end program tabular_output
26Output of the Program
- 1.0000 1.0000 1.0000
- 2.0000 1.4142 1.2599
- 3.0000 1.7321 1.4422
- 4.0000 2.0000 1.5874
- 5.0000 2.2366 1.7100
- 6.0000 2.4495 1.8171
- 7.0000 2.6458 1.9129
- 8.0000 2.8284 2.0000
- 9.0000 3.0000 2.0801
- 10.0000 3.1623 2.1544
27OUTPUT Editing
- A number, called a repeat count, may be placed
before the - i, f, a, L edit descriptors to indicate how
many times they are - to be repeated.
- Thus, the output format could be written in a
more compact form - print (i5,i5,i5,f6.2,f6.2,f6.2,f6.2),
x, y, z, d, e, f, g - Using the repeat count this time
- print (3i5, 4f6.2), x, y, z, d, e, f, g
28read, write and print statements
- A more general form of the read statement
- read (ltci_listgt) ltinput_listgt
- Note that ltci_listgt stands for the control
information list - consisting of the input unit and the format.
- unit specifier External unit to/from data is
transferred -
- unit ltio_unitgt
- unit 5 ! default input device
- unit ! default input device
- format specifier Explicit specification of the
conversion - between internal and external representations of
data. -
- fmt ltformatgt
29read, write and print statements
- Here are two input statements
- read (unit5, fmt) a, b, c
- read (unit, fmt(3f6.3)) a, b, c
- Output is essentially the same, but you use print
or write - statements instead of the read statement
- write (unit6, fmt) a, b, c
- print (unit, fmt) a, b, c
- print , d, e, f
30How to Use More Powerful Format?
- Using the forward slash, /, in read statements
- If you have the following record
- 12345678
- 4567891011
- then the statement
- read (3f2.1,/,3i1), a, b, c, p, q, r
- will read three real numbers from the first line
and three - integers from the second.
31Control Information for Data Transfer
- Data transfer characteristics are specified by
the following items, called control information - The direction of data transfer (input or output).
- The external device (unit) to or from which data
is to be transferred. - The format description that specifies the
conversion between the computer-oriented internal
form of the data and its representation as a
character string.
32The unit specifier
- To transfer data to or from an external file, the
file must be - connected to a unit.
- read(unit 5) a
- read , b
- The unit specifies a processor-dependent unit
number. On input, it is the same unit number that
the processor would use if a read statement
appeared without the unit number. On output/ it
is the same unit number that the processor would
use if a print statement appeared without the
unit number. - A unit number identifies one and only one unit in
an F program. That is, a unit number is global to
an entire program a particular file may be
connected to unit 9 in one procedure and referred
to through unit 9 in another procedure.
33The fmt specifier
- LIST-DIRECTED FORMATTING
- An asterisk format () specifies list-directed
formatting, which is - adequate for input in most situations and for
output in applications - where the precise appearance of the results is
not important. - read(unit5,fmt) a, b
- write(unit6,fmt) a, b
34Explicit format control
- The following specification describes the
arrangement of five fields within a record - (3f12.0, 2i3)
- An input field can be specified by the following
format code - read (unit, fmt(3e12.0, 2i3)) A, B, C, I, J
- Each format code in a format description includes
the following information - 1) a count, indicating the number of consecutive
fields to which it corresponds - 2) a conversion mode, indicating by letters such
as f, e, i, L or a. - 3) a field width
- 4) a decimal position (optional)
- F format code for integer input
- read(unit , fmt (i3, i4, 2i6, i8,
i3))I,J,K,L,M,N - external appearance field width
format code internal value - 123 3
i3
123 - 123 4
i4
123 - -123 6
i6 -123 - -1203 6 i6
-1203 - -12300 8
i8 -12300
35Explicit format control
- F format code for real output
- write(unit ,fmt (f10.3 f6.0,f8.2,f5.2,
f6.3),A,B,B,C,H - real variable internal value format code
external appearance - A -897.6577 f10.3 _ _
-897.658 - B 234. f6.0 _ _
234. - B 234. f8.2 _ _
234.00 - C -0.12 f5.2
-0.12 - H 0. f6.3 _
0.000
36Repetitive I/O
- Inum11 Jnum1231
- Inum22 Jnum2345
- read (unit1, fmt(4i3)) Inum1,Jnum1,Inum2,Jnum2
- write(unit2,fmt2(i3,i5))Inum1,Jnum1,Inum2,Jnum
2 - fmt2(i3,i5) is the same as fmt(i3,i5,i3,i5)
- fmt(4i3) is the same as fmt(i3,i3,i3,i3)
- ----------------
- 1 231 2 345
37Format longer than list
- read (unit1,fmt(3i3)) Inum1,Jnum1,Inum2
- write (unit2,fmt2(i3,i5))Inum1,Jnum1,Inum2
- output ex
- ----------------
- 1 123 2
38List longer than format
- read (unit1,fmt(3i3)) Inum1,Jnum1,Inum2,Jnum2
- write (unit2,fmt2(i3,i5))
- Inum1,Jnum1,Inum2,Jnum2,
- Inum3,Jnum3, Inum4,Jnum4
- format rescan
- Output ex
- -----------------
- 1 123 2 34
- 3 22 4 443
39Unformatted record
- An unformatted record consists of a sequence of
values (in processor-dependent form) -
- An unformatted record can only be read by an
unformatted input statement - read(unit9) d,e,f
- write(unit9) A,B,C
- produces unformatted records for the values
of A, B and C. -
40Connecting an external file to aprogram and
disconnecting it
- For any information to be transferred between a
file and a - program the file must be connected to a unit.
- This connection is initiated by means of an open
statement - open(ltopen_specifier_listgt)
- The ltopen_specifier_listgt consists of
- unit ltunit_numbergt
- file ltfile_namegt
- status ltfile_statusgt
- fmt ltformat_modegt
- action ltallowed_actionsgt
- position ltfile_positiongt
41The status specifier
- The status specifier is not optional.
- It specifies what the status of the file is
before the file is opened by the program - status
ltfilestatusgt - ltfilestatusgt is a character expression which is
one of old, new, - replace or scratch.
- Eg statusold statusnew
- If the status is old the file must already exist,
whereas if it is new then it must not already
exist. If new is specified then a new file is
created. If it is replace and the file already
exists, then it is deleted and an attempt is made
to create a new file with the same name. If this
is successful, the status is changed to old. If
it is replace and the file doesnt exist, the
action will be the same as if new had been
specified. If ltfilestatusgt is scratch then a
special unnamed file is created for use by the
program until when the program ceases execution.
Such a file can be used as a temporary file for
the duration of execution.
42The action specifier
- As well as specifying the initial status of the
file, it must also be - specified what types of I/O operations are
allowed with the file. - The action specifier is used for this purpose
- action ltallowed_actionsgt
- ltallowed_actionsgt is a character expression which
must take - exactly one of read, write or readwrite.
- If ltallowed_actionsgt is read then file is to be
treated as a - read-only file.
- If ltallowed_actionsgt is write then file is to be
treated as an - output file.
- If ltallowed_actionsgt is readwrite then all I/O
are allowed.
43The access specifier
- The access specifier is optional.
-
- The access specifier specifies the access type
that is permitted to the file - access ltaccess_typegt
- where ltaccess_typegt is a character expression
which must take one of the two values sequential
or direct. - If ltaccess_typegt is not specified, then the
default is (it is understood to be) sequential. - If ltaccess_typegt is specified to be sequential, a
position - specifier must be included to instruct the open
statement where - the file is to be initially positioned. This is
discussed next.
44The position specifier
- The position specifier is optional
- position ltfile_positiongt
- where ltfile_positiongt is a character expression
which - must take one of the values rewind or
append - If the file did not previously exist and
ltfile_positiongt - is rewind then the file is positioned at its
initial point. - If the file does exist and ltfile_positiongt is
rewind - then the file is positioned at its initial
point and read statements - read the first record in the file, and write
statements write a new - first record.
- If the file exists and ltfile_positiongt is append
then the file is - positioned just before the endfile record.
45 files
- Files normally have a name by which they are
known to the computer - system
- file ltfile_namegt
- where ltfile_namegt is a character expression
which must obey the - rules for of a file name for the particular
computer system. - Thus, if the name of the file is given as semih,
the statement to connect to - this file can be given as
- open(unit9, filesemih, statusold,
- actionread, position rewind)
46The open statement
- The open statement establishes a connection
between a unit and an external file and
determines the connection properties. - After this is done, the file can be used for data
transfer (reading and writing) using the unit
number. Eg - open (unit6,fileMy_Data.bin,statusold,
- action read, form unformatted)
- open (unit8,fileMy_Input.txt,status old,
- action read)
- open(unit31, fileX23, status old,
- action read)
47The close statement
- Execution of a close statement terminates the
connection of a file to a unit. - Any connections not closed explicitly by a close
statement are closed by the operating system when
the program terminates. - The form of the close statement is
- close(ltclose_spec_listgt)Eg
- close(unit9)