Title: Input / Output
1Chapter 5
2Control over input output
- The input and output are basically facilitates a
communication between the user and the program.
External media
Computers memory
Keyboard Disk file Magnetic type etc.
Real Integer Character etc.
Input
Convert to internal form
Display Printer Disk file Magnetic type etc.
Output
Convert to external form
Input and output editing
3Types of I/O Statements
- There are two basic types of I/O statements
- in Fortran 90
- 1) List-directed (Without Formatted)
- 2) Formatted (Programmer Formatted)
4Formatted Output
- The simpler of two output statements in Fortran
is - print statement
- print format-specifier,
output-list - where format-specifier is one of the following
- 1) (an asterisk)
- 2) A character constant or a character variable
(or expression or array) whose value specifies
the format for the output. - 3) The label of a format statement
- print statement displays the values of the items
in the output-list on a new line.
5Formatted Output
- The format-specifier specifies the format in
which values of the expressions in the output
list are to be displayed. - In the second type of format-specifier, the
formatting information is given as a character
string that consists of format descriptors,
separated by commas and enclosed in parentheses - (list of format descriptors) or (list of
format descriptors) - In the third case, the formatting information is
supplied by a format statement whose label is
specified. This statement has the form - label format (list of format
descriptors) - where label is an integer in the range 1 through
99999.
6Formatted Output
- For some Fortran compilers, the first character
of each line of output directed to a printer is
used to control the vertical spacing. - black Normal spacing advance to the next line
before printing - 0 Double spacing skip one line before
printing - 1 Advance to top of next page before
printing - Overprint the last line printed
- There are many format descriptors that may be
used in format specifiers. A list is presented in
the following table.
7Format Descriptors
8Format Descriptors
9Formatted Output
- Example
- print (I3) , N
- The format descriptor I3 specifies that the value
to be printed is an integer and is to be printed
in the first three positions of a line. If the
value of N is 15, the three positions are filled
with _15 (where _ denotes a blank).
10Integer Output- The I descriptor
- Integer values that are output using an I
descriptor are right-justified in fields of the
specified sizes that is each value is displayed
so that its last digit appears in the rightmost
position of the field. For example, if the values
of the integer variables - Number, L, and Kappa are
- Integer Number 3, L 5378, Kappa -12345
- then the statements
- print (1X, 2I5, I7, I10) , Number, Number -3,
L, Kappa - print (1X, 2I5.5, I7, I10.7) , Number, Number
-3, L, Kappa - print (1X, 2I5.0, I7, I10) , Number, Number -3,
L, Kappa - or
11Integer Output- The I descriptor
- Print 30, Number, Number -3, L, Kappa
- Print 31, Number, Number -3, L, Kappa
- Print 32, Number, Number -3, L, Kappa
- 30 format (1X, 2I5, I7, I10)
- 31 format (1X, 2I5.2, I7, I10.7)
- 32 format (1X, 2I5.0, I7, I10)
- produce the following output
- _ _ _ _ _ 3 _ _ _ _ 0 _ _ _ 5378 _ _ _ _ -12345
- _ _ _ _ 0 3 _ _ _ 0 0 _ _ _ 5378 _ _ - 0 012345
- _ _ _ _ _ 3 _ _ _ _ _ _ _ _ 5378 _ _ _ _ -12345
12Real Output- The F, E, ES and EN Descriptors
- Integer and real outputs are right-justified in
their fields. In a Fw.d descriptor, w specifies
the total width of the field in which to display
a real value, and d is the number of digits to
the right of the decimal point. - Example
- integer In 625, Out -19
- real A 7.5, B 0.182, C 625.327
- using the statement
- print (1X, 2I4, 2F6.3, F8,3), In, Out, A, B, C
- The resulting output is
- _ _ 6 2 5 _ - 1 9 _ 7 . 5 0 0 _ 0 . 1 8 2 _ 6 2
5 . 3 2 7
13Real Output- The F, E, ES and EN Descriptors
- For a descriptor Fw.d, one should have w gt
d3 to allow for the sign of the number, the
first digit, and the decimal point (-,0.). - Lets see the E descriptor with an example
- The values of real variables A, B, C and D
are given by - real A0.12345E8, B0.0237, C4.6E-12,
D-76.1684E12 - the statement
- print (1X, 2E15.5, E15.4, E14.4), A, B, C,
D - produces output like the following
- _ _ _ _ _0.12345E08_ _ _ _0.23700E-01_ _ _ _
_0.4600E-11_ _ _-0.7617E14 - As with the F descriptor, a field is
asterisk-filled if it is not large enough for the
value.
14Real Output- The F, E, ES and EN Descriptors
- The scientific descriptor ES is used in the same
manner as the E descriptor. Only difference is
that the values are normalized. - The engineering descriptor EN also is used in the
same manner as the E descriptor. Only difference
is that exponent is constrained to be a multiple
of 3.
15Character Output
- Character data may also be displayed by using an
A format descriptor of the form rA or rAw. In the
first form, the field width is determined by the
length of the character value being displayed. - In the second form, if the field width exceeds
the length of the character value, that value is
right-justified in the field.
16Positional descriptors - X and T
- A descriptor of the form nX can be used to insert
n blanks in an output line. A tab descriptor of
the form Tc causes the next output field to begin
at the specified position c on the current line. - For example
- print 75, John Q. Doe, CPSC, Number
- 75 format (1X, A11, 3X, A4, 2X, I3)
- or
- 75 format (1X, A11, T16, A4, 2X, I3)
- will produce the output
- _ John_Q._Doe_ _ _CPSC_ _141
17Repeating Groups and The Slash (/) Descriptor
- It is possible to repeat some format descriptors
by preceding them with a repetition indicator. - For example ? 3F10.2 is equivalent to F10.2,
F10.2, F10.2. - A single output statement can be used to display
values on more than one line, with different
formats, by using a slash (/) descriptor. - Consider the following statement
- print (fmt88) Values, N, A, M, B, C, D
- 88 format (1X, A, 3/ 1X, 2(I10, F10.2) // 1X,
2E15.7)
18Formatted Input
- read statement has two forms the simpler form
is, - read format-specifier, input-list
- For example read 5, I, J, K
- INTEGER INPUT
- Integer data can be read using the I descriptor
of the form rIw, where w indicates the width of
the field. - For example
- integer I, J, K
- read (I6, I4, I7), I, J,
K - or
- read 5, I, J, K
- 5 format (I6, I4, I7)
19Formatted Input
- REAL INPUT
- We can enter a real number in 2 ways
- 1) Enter the numbers without decimal points.
- For example enter the following values for real
variables A, B, C, D and E. - A6.25, B-1.9, C75.0, D.182,
E625.327 - read (F3.2, 2F3.1, F3.3, F6.3),
A, B, C, D, E - enter the data in the form
625-19750182625327 - 2) Enter the decimal point as part of the input
value - read (4F5.0, F8.0), A, B, C, D, E
- enter the data in the following form
- _6.25_-1.9_ _75._.182_625.327
20Formatted Input
- CHARACTER INPUT
- When a read statement whose input list contains a
character variable is executed, all characters in
the field associated with the corresponding A
descriptor are read. - For example Assume the line of data is
- Fourscore_and_seven_years_ago
- and read by statements
- character(6) Speech1,Speech2
- read (2A) , Speech1, Speech2
- the values assigned to Speech1 and Speech2 are
- Speech1 Foursc
- Speech2 ore_an
21Reading Exercises
- ELLIS BOOK
- Do example 3.2 and self-test exercise 3.2 on
pages 60 and 63. - Read pages between 56 and 60.
22Write Statement
- The write statement has the following form
- write (control-list)
output-list - where control-list may include items selected
from the following - 1) A unit specifier, which is a integer
expression whose value designates the output
device - unit unit-specifier or simply
unit-specifier - 2) A format specifier that may be any of the
forms allowed in the print statement. - fmt format-specifier or simply
format-specifier - 3) An ADVANCEclause of the form
- advance character-expression
- where the value of character-expression is either
no or yes. - 4) Any other items that are especially useful in
file processing.
23Read Statement
- The read statement has the following form
- read (control-list) input-list
- where control-list may include items selected
from the following - 1) A unit specifier, which is a integer
expression whose value designates the input
device - unit unit-specifier or simply
unit-specifier - 2) A format specifier that may be any of the
forms allowed in the print statement. - fmt format-specifier or simply
format-specifier - 3) An ADVANCEclause of the form
- advance character-expression
- where the value of character-expression is either
no or yes. - 4) An iostatclause to detect an input error or
end-of-file condition.
24Reading Exercise
- ELLIS BOOK
- Read pages between 230 and 258.
- Study self-test exercises 9.2 and summary.
25File Processing
- Storing/reading large data sets in/from a file
are more convenient for data processing. - OPENING FILES
- In order to open a file in Fortran a unit number
must be connected to it and several items of
information about the file must be supplied. - Open statement is the following form
- open (open_specifier_list)
- where open-list includes
- 1) unit indicating a unit number.
- 2) file (character-expression ) indicating the
name of the file. - 3) status (character-expression ) where the
value of character-expression is one of
26File Processing (Opening files)
- old
- new
- replace
- (or unknown). And scratch
- 4) action allowed_actions
- where allowed_actions are a character expression
whose value is one of - read
- write
- readwrite (default) (statusscratch)
- 5) positioncharacter-expression where the
value of character-expression is one of - rewind (The file at its initial point)
- append (The file at its end)
27File Processing (Opening files)
- asis (Leave its position unchanged)
- 6) IOSTATstatus-variable where
status-variable is an integer variable to which
the value zero is assigned if the file is opened
successfully and a positive value is assigned
otherwise. - NOTE If file name is not presented then
statusscratch must be specified - EXAMPLE
- open (unit 12, File INFO.DAT, status
old, action read, position rewind,
iostat OpenStatus) - where OpenStatus is an integer variable.
- Sometimes it is convenient to declare a character
variable to store the name of file in a way that
the user input the name.
28File Processing (Opening files)
- EXAMPLE
- Character (12) FileName
- write (unit, (1X, A), advance no) Enter
the name of data file - read (unit, fmt) FileName
- and then use the following open statement
- open (unit 12, file FileName, status old,
action read, - position
rewind, iostat OpenStatus) - if the file is successfully opened, the status
variable OpenStatus will be set to 0. - If (OpenStatus gt 0) stop cannot open file
29File Processing (Closing files)
- The close statement has a function opposite that
of the open statement and it is used to
disconnect a file from its unit number. - Close (close-specifier-li
st) - where close-specifier-list must include a unit
specifier and may include other items such as
iostat and status. status may take character
expressions such as keep or delete. - FILE INPUT/OUTPUT
- Once a file has been disconnected to a unit
number, data can be read from or written to that
file using the general forms of the read and
write statements. During this process iostat can
be positive negative or zero. - 1) A positive value in case of an input error
occurs - 2) A negative value if the end of data is
encountered but no input error occurs
30File Processing (File Input/Output)
- 3) Zero value if neither an input error nor the
end of data occurs. - Example
- do
- read (12, fmt, iostat InputStatus) Code,
Temperature, Pressure - ! If end of data, terminate repetition
- if (InputStatus lt 0) exit
- ! If input error, stop execution
- if (InputStatus gt0) stop Input error
- ! Otherwise continue processing data
- Count Count 1
- .
- .
- SumOfPressuresPressure
- end do
31File Processing (rewind/backspace)
- Sometimes it is necessary to reposition a file so
that data values that have already been read can
be read again. - To reposition a file at its beginning use a
rewind statement -
- rewind (auxlist)
- To reposition a file at the beginning of the
proceeding line use backspace statement - backspace (auxlist)
- auxlist consist of a unit specifier and,
optionally, an iostat specifier.
32End of File
- To permit detection of the end-of-file mark, an
exeption specifier of the form iostatInteger
variable may be included in the Control list of
the read statement - read (unit , fmt , iostat
Integer_variable) Input list - The designated Integer variable will be zero
after a normal read operation it will have a
negative value if an end-of-file mark is read. - Example
- integer E_O_F
- do
- read (unit, fmt, iostatE_O_F) Next_Datum
! Set E_O_F - if (E_O_F lt 0) exit
! Test E_O_F - .
- .
- .
- enddo
33Exercises on I/O operations
- ELLIS BOOK
- Read pages between 230-260.
- Do self exercises 9.1 and 9.2 on pages 248 and
254. -
- Exercises on File processing
- Read pages between 264-288.