Title: Introduction to Computing
1Introduction to Computing
2I / O
- There are two types of Input and Output
- statements
- list-directed free-format, extra spaces
- programmer-formatted
- Format effects only the display of variables not
their values through program execution - A format can specify both the horizontal and
vertical positions of the variables on the paper
and also significant digits to be printed.
3Formatted Output
- print format-specifier , output-list
- write (, format-specifier) output-list
- format-specifier is one of the following
- an asterisk, - indicates list-directed
output in which the format for the expressions in
the output-list is machine dependent - a character string that consists of format
descriptors, seperated by commas and enclosed in
parantheses - (list of format descriptors) or
- (list of format descriptors)
4Formatted Output
- print format-specifier , output-list
- write (, format-specifier) output-list
-
- label of a format statement - an integer in the
range 1 through 99999 - each execution of a print or write statement
displays the values of the items in the
output-list on a new line - Do not forget to use commas to seperate variable
from its neighbors
5Formatted Output
- format statement can be written in several ways
1- Write(,100) i,x 100 Format(1x,I6,F10.2)
2- character(len20) string string(1x,I6,F10.2
) Write(, string) i,x
3- Write(, 1x,I6,F10.2) i,x
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. - blanck 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
7Format descriptors
- There are many format descriptors that may be
used in format specifiers - a most commonly used
list is presented in the following tables - Forms Use
- rIw rIw.m Integer data
- rFw.d Real data in decimal notation
- rEw.d Real data in exponential notation
- rA rAw Character data
- rLw Logical data
8Format descriptors - more
- Forms Use
- ESw.d ESw.dEe Real data in scientific notation
- ENw.d ENw.dEe Real data in engineering notation
- x x x x Character strings
- nX Horizontal spacing
- / Vertical spacing
9Format descriptors
- Here
- w a positive integer specifying field width
- m minimum number of digits to be
read/displayed - d number of digits to the right of the decimal
point - e number of digits in an exponent
- n number of spaces to skip
- x a character
- r repeated count
- c column number
10Integer output - I descriptor
- right-justified - each value is displayed so
that its last digit appears in the rightmost
position of the field of a specified size - rIw rIw.m
- example
- integer Number 3, L 5378, Kappa -12345
- Write(,(1X, 2I5, I7, I10)) , Number, Number
-3, L, Kappa - Write(,(1X, 2I5.5, I7, I10.7)), Number, Number
-3, L, Kappa - Write(, (1X, 2I5.0, I7, I10)) , Number, Number
-3, L, Kappa
11Integer output - I descriptor
- example
- 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)
- will produce the following output (where _
denotes a blank) - _ _ _ _ _ 3 _ _ _ _ 0 _ _ _ 5378 _ _ _ _ -12345
- _ _ _ _ 0 3 _ _ _ 0 0 _ _ _ 5378 _ _ - 0 012345
- _ _ _ _ _ 3 _ _ _ _ _ _ _ _ 5378 _ _ _ _ -12345
12Integer output - I descriptor
- if an integer value (including the minus sign)
requires more space than specified by a
descriptor, the field is filled with asterisks - print (1X, 4I3) , Number, Number - 3, L,
Kappa - will produce
- _ _ _ 3 _ _ 0
13Real output - F, E, ES and EN descriptors
- - real outputs are right-justified in their
fields - - sometimes numbers migt be rounded off in
display - rFw.d
- integer In 625, Out -19
- real A 7.5, B 0.182, C 625.327
- Write(,44), In, Out, A, B, C
- 44 format (1X, 2I4, 2F6.3, F8.3)
- will produce the following output
- _ _ 6 2 5 _ - 1 9 _ 7 . 5 0 0 _ 0 . 1 8 2 _ 6 2
5 . 3 2 7
14Real output - F descriptor
- for Fw.d descriptor
- w ? d 3
- to allow space for the sign of the number,
- a first digit,
- and a decimal point
15Real output - E descriptor
rEw.d Real data in exp. Notation the real
variables are normalized to a range betw. 0.1 to
1.0 when displayed in exponential notation with
E.
- real A0.12345E8, B0.0237, C4.6E-12,
D-76.1684E12 - print (1X, 2E15.5, E15.4, E14.4), A, B, C, D
- will produce the following output
- _ _ _ _ _0.12345E08_ _ _ _0.23700E-01_ _ _ _
_0.4600E-11_ _ _-0.7617E14
16Real output - E descriptor
- for Ew.d descriptor
- w ? d 7
- to allow a space for the sign of the number,
- a leading zero,
- a decimal point,
- and E with the exponent
17Real output - ES and EN descriptors
- ES - scientific descriptor used in the same
manner as the E descriptor, but the values are
normalized so that the mantissa is at least 1 but
less than 10 (unless the value is zero) (ESw.d) - EN - engineering descriptor used in the same
manner as the E descriptor, but exponent is
constrained to be a multiple of 3 so that a
nonzero mantissa is greater than or equal to 1
and less than 1000. (ENw.d)
18Character output
- rAw
- character data may be displayed by including
them in a list of descriptors of a format
specifier - example
- real A 0.3, B 7.9
- print (1X, A , F6.2, B , F6.2), A, B
- or
- print 70, A, B
- 70 format (1X, A , F6.2, B , F6.2)
- will produce the following output
- _ A _ _ 0 . 3 0 B _ _ 7 . 9 0
19Character output
- rAw
- character data may be displayed by including
them in a list of descriptors of a format
specifier - Note
- if width is longer than the variable length the
variable will be printed out right justified in
the field - Otherwise, only the first w characters will be
printed out in display
20Character output
- example
- print (1X, A, F6.2, A, F6.2), A, A, B, B
-
- or
- print (1X, 2 (A, F6.2) ), A, A, B, B
- also 2F6.2 is equivalent to F6.2, F6.2
21- example
- character (len17) stringthis is a string.
- write (,75) string
- write (,76) string
- write (,77) string
- t h i s i s a s t r i n g .
- 75 format(1x,A) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ 5 10
15 20 - t h i s i s
a s t r i n g . - 76 format(1x,A20) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ 5 10
15 20 - t h i s i
- 77 format(1x,A6) _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ 5 10
15 20
22Logical output
- rLw
- output is T or F
- Example
- logicalout.TRUE. , b.FALSE.
- Write(,22) out,b
- 22 format(1x,2L5)
- _ _ _ _ T _ _ _ _ F
23Positional descriptors - X and T
- X inserts spaces between variables
- T inserts tabs over a specific column
- nX, Tc
- example
- integer Number 141
- write(,75) John_Q. _ Doe, ABCD, Number
- 75 format (1X, A11, T16, A4, 2X, I3)
- will produce the following output
- _ John_Q. _ Doe _ _ _ A B C D _ _ 141
24Changing Output Lines - /
- To skip lines / operator is used
- example
- integer i10
- real time 300., depth330.,amp850.65,ph
ase30. - write(,76) i, time,depth,amp,phase
- 76 format (1X, T20,Results for test number ,
I3,///, - 1x, Time, F7.0,
- 1x, Depth, F7.1,meters,/,
- 1x, Amp , F8.2,/,
- 1x, Phase, F7.1)
- will produce the following output
-
25Formatted input
- READ format-specifier , input-list
- integer input
- integer I, J, K
- read (I6, I4, I7), I, J, K
- or
- read 5, I, J, K
- 5 format (I6, I4, I7)
- if the data entered is
- _ _ - 1 2 3 _ _ 4 5 _ _ _ 6 7 8 9
- the values assigned to I, J and K are
- I - 123 J 45 K 6789
26Formatted input
- READ format-specifier , input-list
- real input
- enter the numbers without decimal points
- example (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 data in the following form
- 625-19750182625327
- 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
27Character input
- the line of data
- Fourscore_and_seven_years_ago
- is read by statements
- character(6) Speech1, Speech2
- read (2A) , Speech1, Speech2
- values assigned to Speech1 and Speech2 are
- Speech1 Foursc
- Speech2 ore_an
28Character input
- Note
- if the width w is larger , the data from the
right-most portion of the field is loaded into
character variable. - if the width w is smaller , the charactersin the
field will be stored in the left-most characters
of the variable and the remainder will be blank.
Example Character(len10)
29Character input
Example Character(len10)string1,string2 Charac
ter(len5)string3 Character(len15)string4,str
ing5 Read(,(A)) string1 Read(,(A10))
string2 Read(,(A10)) string3 Read(,(A10)) s
tring4 Read(,(A)) string5
input ABCDEFGHIJKLMNO Ouput String1
ABCDEFGHIJ String2 ABCDEFGHIJ String3
FGHIJ String4 ABCDEFGHIJ_____ String5ABCDEF
GHIJKLMNO
30Horizontal Positioning
X skip over fields T jump to column number. it
can read same data twice
Example Character(len6) string Integerin Re
ad(,(I6,T1,A6)) in,string
input 123456 Output in 123456 String
123456
31Vertical Positioning
/ descriptor serves to skip new lines
input 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 Outp
ut a 1.0 b 2.0 c 7.0 d 8.0
Example real a,b,c,d Read(,66) a,b,c,d 66
format(2f10.2,//, 2f10.2)
32write statement
- write (control-list) output-list
- control-list items
- unit specifier - integer expression whose value
designates - the output device (or an asterisk, indicate the
standard - output device, monitor screen or printer)
- UNIT unit-specifier or unit-specifier
- format specifier - any of the forms allowed in
the print - statement
- FMT format-specifier or format-specifier
33write statement
- write (control-list) output-list
- control-list items (continued)
- ADVANCE clause
- ADVANCE character-expression
- where the value of character-expression is
either NO or YES - any other items that are especially useful in
file processing
34read statement
- read (control-list) output-list
- control-list items
- unit specifier
- format specifier
- advance clause
- iostat clause - to detect input error or
end-of-file condition - examples
- read (5, ) a, b, c
- read (unit5, fmt ) a, b, c
- read (in, ) a, b, c where in has a value of 5
-
35File processing
- opening files
- open (open-list)
- open list includes
- unit specifier - unit number.
- file clause - character-expression indicating
the name of file - status clause - character-expression with a
value of OLD or NEW or REPLACE etc. - action clause - i-o-action with a character
expression of value READ or WRITE or
READWRITE (default)
36File processing
- position clause - character-expression with a
value of REWIND (file at initial point) - APPEND (file at the end)
- ASIS (position unchanged)
-
- iostat status-variable - an integer variable
to which zero is assigned if the file is opened
successfully and a positive value is assigned
otherwise.
37File processing
- example
- open (unit 12, File INFO.DAT, status
OLD, - action READ, position REWIND,
- iostat OpenStatus)
- OpenStatus is an integer variable.
-
38Opening files
- Character (12) FileName
- write (unit, (1X, A), advance NO) Enter
data file name - read (unit, fmt) FileName
- open (unit 12, file FileName, status OLD,
- action read,
- position rewind, iostat OpenStatus)
- ! if file is successfully opened, OpenStatus will
be set to 0 - if (OpenStatus gt 0) stop cannot open file
39Closing files
-
- Close (close-list)
- where close-list must include a unit specifier
and may include other items such as iostat, err
and status
40File input/output iostat
-
- During read and write processes from a connected
unit iostat can take different values - positive value - an input error occured
- negative value - end of data is encountered, no
input errors - zero value - non of the above
41iostat
- example
- real Temperature, Pressure, SumOfPressures0.0
- 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
- SumOfPressures SumOfPressures Pressure
- end do
42File processing
- to reposition a file at its beginning use a
rewind statement -
- rewind unit-number
- to reposition a file at the beginning of the
preceding line use backspace statement - backspace unit-number
43Homework 4
- PART 1. Given the statements,
- real a
- character (LEN2) string
- logical ok
- read ( , '(F10.3, A2, L10) ) a, string, ok
- what would be read into a, string and ok if the
following input - records were used?
- (a) bbb5.34bbbNOb.true.
- (b) 5.34bbbbbbYbbFbbbbb
- (b) b6bbbbbb3211bbbbbbT
- (d) bbbbbbbbbbbbbbbbbbF
- where b represents a space or a blank character.
44Homework 4
- PART 2.
- Write a Fortran program which will prompt the
- user for a file name, open that file and then
- read the file line by line outputting each line
to - the screen prefixed with a line number.
-
45Homework 4
- send your homework to
-
- http//homework.itu.edu.tr