260 likes | 401 Views
CS1001 Lecture 22. Formatted Input and Output. Types of Input/Output. List Directed PRINT *, “Temperature = “, Temperature READ *, Choice User-Formatted PRINT ‘(F8.2)’, Temperature PRINT 120, Temperature 120 FORMAT (F8.2) READ ‘(I4, I6)’, Input1, Input2 READ 130, Input1, Input2
E N D
CS1001 Lecture 22 • Formatted Input and Output
Types of Input/Output • List Directed • PRINT *, “Temperature = “, Temperature • READ *, Choice • User-Formatted • PRINT ‘(F8.2)’, Temperature • PRINT 120, Temperature 120 FORMAT (F8.2) • READ ‘(I4, I6)’, Input1, Input2 • READ 130, Input1, Input2 130 FORMAT (I4, I6)
PRINT Statement • PRINT format-specifier, output-list • format-specifier is • *, an asterisk (format is compiler-dependent) • a character constant or variable • the label of a FORMAT statement • FORMAT statement can apply to more than one PRINT statement • FORMAT statement should follow PRINT statement for readability • output-list is an expression or list of expressions separated by commas
Integer Output • rIw or rIw.m (right justified) • r - repetition indicator, indicating the number of fields • I - denotes integer data • w - number of spaces to display data • m - minimum number of digits to display Example of 4I5.2: ^1234^^567^^^89^^^00 Example of 4I5: ^1234^^567^^^89^^^^0 5 5 5 5 5 5 5 5
Real Output • rFw.d (right justified) • r - repetition indicator, indicating the number of fields (right-justified) • F - denotes real (floating point) data • w - total width of the field • d - digits to right of decimal point • Example of 2F8.3: ^123.456^^^0.789 8 3 8 3
Real, Exponential Notation • rEw.d • r - repetition indicator, indicating the number of fields • E - real data, exponential notation, • 0.0 <= mantissa < 1.0 • w - total width of field • d - digits to right of decimal point Example of 2E15.5: ^^^^0.12345E+08 ^^^^0.23700E-01 15 5 15 5
Character Output • rA or rAw • r - repetition indicator • w - width (optional) Example: X = 12.3456 Y = -12.3456 PRINT 264, ‘X = ‘, X, ‘ Y = ‘, Y 264 FORMAT(1X, A, F6.2, A, F6.2) X = 12.35 Y = -12.35 ^ ^ ^^ ^ ^ ^ 1X, A, F6.2, A, F6.2
Positional Descriptors • 1X at beginning of descriptor list denotes single line spacing AND single blank space • nX elsewhere in descriptor list denotes number of blank spaces to be printed • Tc “tabs” to the cth space on the line, so data begins there (absolute space number, but first space is blank)
Control Characters • 1X or ‘ ‘ (blank) for normal spacing • ‘0’ for double spacing • ‘1’ for advancing to a new page • ‘+’ for overprinting current line • Printer/system dependent, may not be in effect Slash Descriptor • / causes output to begin on a new line • /// would cause two blank lines to be inserted before data output
Format Descriptors - Summary • Iw or Iw.m for integer data • A or Aw for character data • ‘x...x’ or “x...x” for character strings • Fw.d for real data in decimal notation • Ew.d real exponential notation • Tc tab descriptors • nX - inserts n blanks in an output line • / - vertical spacing
WRITE Statement • WRITE (control-list) output-list • control-list includes: • a unit specifier indicating the output device • UNIT = unit-specifier • or simply unit-specifier • a format-specifier • FMT = format-specifier • or simply format-specifier • output-list is same as for PRINT statement
Unit Numbers • Standard Input device is the terminal keyboard or UNIT 5 • Standard Output device is the terminal display monitor or UNIT 6 • Standard Error device is the terminal display monitor or UNIT 0
Equivalent WRITE Statements • All of which are equivalent : PRINT *, Time,Temp WRITE (6, *) Time, Temp WRITE (6, FMT = *) Time, Temp WRITE (UNIT = 6, FMT = *) Time, Temp WRITE (NOUT, *) Time, Temp where NOUT = 6 WRITE (UNIT = NOUT, FMT = *) Time, Temp
READ Statement • READ format-specifier, input-list • format-specifier is: • * (an asterisk) • a character variable or constant • the label of a FORMAT statement • use the first form for this class until we get into file handling • input-list is a variable or list of variables
General READ Statement • READ (control-list) input-list • input-list is same as for READ statement • control-list includes: • a unit specifier indicating the output device • UNIT = unit-specifier • or simply unit-specifier • a format-specifier • FMT = format-specifier • or simply format-specifier
READ Statements • All of which are equivalent : READ *, Time, Temp READ (5, *) Time, Temp READ (5, FMT = *) Time, Temp READ (UNIT = 5, FMT = *) Time, Temp READ (NIN, *) Time, Temp where NIN = 5 READ (UNIT = NIN, FMT = *) Time, Temp
Format Descriptors 1/2 • Iw or Iw.m for integer data • Bw or Bw.m for integer data in binary form • Ow or Ow.m for integer data in octal form • Zw or Zw.m for integer in hexadecimal • A or Aw for character data • ‘x...x’ or “x...x” for character strings • Lw for logical data
Format Descriptors 2/2 • Fw.d for real data in decimal notation • Ew.d or Ew.dEe real exponential notation • 0.0 <= mantissa < 1.0 • ESw.d or ESw.dEe real scientific notation • 1.0 <= mantissa < 10.0 (unless = 0.0) • ENw.d or ENw.dEd real engineering notat. • 1.0 <= mantissa < 1000.0, exponent multiple of 3 • Gw.d or Gw.dEe general i/o descriptor
Descriptor Meanings • w - positive integer constant - width • m - nonnegative integer constant - minimum number of digits to be displayed • d - nonnegative integer constant - digits to right of decimal point • e - nonnegative integer constant - digits in exponent • x - a character
Other Integer Representation • Given the integer value of “200” decimal • Format B9 produces “^11001000” • Format O4 produces “^310” • Format Z3 produces “^C8”
Do Not Worry About • S, SP and SS optional output plus sign controls: • SP specifies that optional plus signs are to be printed • SS suppresses optional plus signs • S restores the processor option • BN and BZ blank character controls • BN treats blanks as nulls, and ignores them • BZ treats blanks as zeros • kP scale factor control (2P treats input of 123 as 1.23 internally
Other Format Descriptors • General descriptor Gw.d and Gw.dEe are the same as Iw format, values of d and e are ignored. • Relative tab descriptors TRw and TLw: • TRw tabs right w positions and is identical in operation to wX • TLw tabs left w positions and is a form of backward tabbing, which is to be avoided • There is no reason to use any of these
Real, Scientific Notation • rESw.d or rESw.dEe • r - repetition indicator, indicating the number of fields • ES - real data, scientific notation • w - total width of field • d - digits to right of decimal point • e - number of positions to display exponent Example of 2ES15.5: ^^^^1.23450E+08^^^^2.37000E-01
Real, Engineering Notation • rENw.d or rENw.dEe • r - repetition indicator, indicating the number of fields • EN - real data, engineering notation • w - total width of field • d - digits to right of decimal point • e - number of positions to display exponent Example of 2EN15.5: ^^123.45000E+06^^237.00000E-03
Special Descriptors • Tc, TLn, TRn - tab descriptors • c - positive integer constant representing character position • n - positive integer constant specifying number of character positions • nX - inserts n blanks in an output line • / - vertical spacing