420 likes | 431 Views
Week 10. More Control over INPUT and OUTPUT (I / O). More Control over INPUT and OUTPUT (I / O).
E N D
Week 10 More Control over INPUT and OUTPUT (I / O)
More Control over INPUT and OUTPUT (I / O) The input and output facilities of any programming language are extremely important, because it is through these features of the language that communication between the user and the program is carried out. F , therefore, provides facilities for input and output at two quite different levels. As are given before, list-directed input and output statements that provide the capability for straightforward input from the keyboard and also the output to the display or printer. These list-directed “i/o” - statements, however, allow the user very little control over the source or layout of the input data.
More Control over INPUT and OUTPUT (I / O) • F allows the programmer, • to specify exactly how the data will be presented and interpreted, from which of the available input units it is to be read • how the results are to be displayed • to which of the available output units the results are to be sent.
INTERFACE between the USER and the COMPUTER • Considering the following 9 – digit input data there are enormous number of possible interpretations : • Data : 1 2 3 4 5 6 7 8 9 • 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, 0.45, 67, 8900 • etc.
FORMATS and EDIT DESCRIPTORS • An input statement must contain three distinct types of information • where the data is to be found • where it is to be stored in the computer’s memory • how it is to be interpreted • Similarly, an output statement must define • where the results are currently stored • where they are to be sent • in what form they are to be displayed.
INPUT EDITING • EXAMPLES of INPUT DATA : 123456789 • read “ ( i 3, i 3, i 3 )” , n1, n2, n3 • ! n1 = 123, n2 = 456, n5 = 789 • read “ ( t 4, i 2, t 8, i 2, t 2, i 4 )” , x, y, z • ! x = 45, y = 89, z = 2345 • read “ ( f 9.4 )” , real_num ! real_num = 12345.6789 • read “ ( f 3.1, f 2.2, f 3.0 )” , r1, r2, r3 • ! r1 = 12.3, r2 = 0.45, r3 = 678.0
EXAMPLE for formatting of real numbers using “ f – edit descriptor “ depending on the two different INPUT DATA : • read “ ( f 3.1, f 2.2, f 3.0 )” , s1, s2, s3 • DATA 1DATA 2 • 123456789 .23.56.8 • ---------------------- ----------------- • s1 contains : 12.3 0.23 • s2 contains : 0.45 0.5 • s3 contains : 678.0 6.8
EXAMPLEs for formatting of an input real number in different ways using “ a – edit descriptor “ • realnumber is : 361.764 • 361.764 • 3.61764+2 • 361764–3 • 0.0361764e4 • 3617.64d-1 • 3.61764 E +2 • 361.764+0
EXAMPLEs for formatting of an input – character type using “ a – edit descriptor “ • character ( len = 10 ) : : ch1 • character ( len = 6 ) : : ch2 • character ( len = 15 ) : : ch3 • Depending on the above-given declaration the formats may have the following forms in read statements : • read “ ( a10, a6, a15 )” , ch1, ch2, ch3 • or • read “ ( a, a, a )” , n1, n2, n3
LOGICAL DATA - INPUT EDITING The edit descriptor is used with logical data, and takes the following form, where upper case “ L” is used to avoid the potential confusion with the digit “ 1 “ that can be caused to human readers by using the lower-case form : L w “ w “ caharacters are used to derive either a “ true “ value, a “ false “ value or an error.
LOGICAL DATA - INPUT EDITING • Any of the following are acceptable as representing true : • t • true • .T • .true. • truthful • while the following will all be interpreted as false : • F • False • .f. • futile
OUTPUT EDITING The following figure shows the main edit descriptor that are available for output. EXAMPLE for “i” edit descriptor tom = 23 dick = 715 harry = -12 print *, “( i 5, i 5, i 5 )”, tom, dick, harry This statement produce following line of output (where the symbol # represents a space ) : ###23##715##-12
OUTPUT EDITING EXAMPLE for “f” edit descriptor : x = 3.14159 y = -275.3024 z = 12.9999 print *, “( f 10.3, f 10.3, f 10.3 )”, x, y, z This statement produce following line of output (where the symbol # represents a space ) : #####3.142##-275.302####-13.00
OUTPUT EDITING EXAMPLE : program tabular_output real, parameter : : third = 1.0 / 3.0 real : : x integer : : i do i = 1, 10 x = 1.0 print “( f 15.4, f 15.4, f 15.4 )”, x, sqrt (x), x**third end do end program tabular_output
OUTPUT EDITING In the above-given program the same edit descriptor has been repeated several times. A number, called a “repeat count”, may be placed before the “ i, f, a or L “ – edit descriptors to indicate how many times they are to be repeated . Depending on this, the format could be written more succinctly and İt is possible to write it more clearly. EXAMPLE : print “( i 5, i 5, i 5, f 6.2, f 6.2, f 6.2, f 6.2 )”, x, y, z, d, e, f, g or better print “( 3 i 5, 4 f 6.2 )”, x, y, z, d, e, f, g
Introduction • The most convenient way to process involving large data sets is to store them into a file for later processing. • In order to work with files we need to focus on I/O operations, data transfer and file operations.
Information for data transfer • Data transfer characteristics are specified by the following items called control information. (1) The direction of data transfer (input or output) (2) The external device, or unit, to or from which data is to be transferred (3) The format description that specifies conversion between the computer-oriented internal form of data and its representation as a character string. (4) (Optional) Positioning action that is to occur at the end of data transfer (Advancing; if the file on the external device is to be repositioned to the end of the current record. Non advancing; if the file position is to remain in its place within the current record) (5) (Optional) Exception handling to be performed in the event of an end-of-file, end-of-record, or error condition during data transfer.
I / O Statements Direction of Transfer The direction of transfer is determined by the initial keyword in the statement. The keyword read denotes input, while write denotes output. External Device Each external device is identified with a unique integer value called a unit number. A given unit number represents the same device in the main program and in all of its modules and subprograms. Thus, unit numbers may be said to have global scope.
I / O Statements I / O Statement Forms An input or output statement has the following form. read (unit=2, fmt=”(2i5, a, 2f16.8)”) I, Code, Str, Bugle, Dpr write (unit=*, fmt=”(f10.3, i6, f10.3,a)”, advance =”no”) A, B, C,” # ” write (unit=*, fmt=*) Code, X+Y+Z The positioning specifier advance=”no” in the write statement states that the output file is not to be advanced to a new record after A, B and C have been written to the file. Thus the second write statement will append additional data to the same output record.
Data Transfer RECORDS Information on a file subdivided into records. A record corresponds roughly to a line on a terminal screen or to a printed line. The structure of unformatted records is processor-dependent, since the amount of space required on the external unit depends on the details of processor data representation. FORMATTED RECORDS A formatted record is composed of characters. These characters may include letters, digits, and special symbols from the ASCII character set. NONADVANCING INPUT AND OUTPUT Input and output always positions the input or output file, as a default option, at the end of the last record to or from which information has been transferred. In some cases this style of input and output is not convenient. It would often be preferable to stop data transfer in the middle of an input or output record and to continue later with the remainder of the same record. Instead of default advancing file-positioning style, output statement may request a non advancing style, whereby the file position may remain within a record after the input or output statement has been executed. (See example 9.5, p400, Meissner`s book)
Format specifier LIST-DIRECTED FORMATTING An asterisk format specifier indicates 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
Explicit format control The following specification describes the arrangement of five fields within a record (3 f12.0, 2 i3) An input field can be specified by the following format code read (unit=*, fmt=“(3 e12.0, 2 i3)”) 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, es, i, l or a. 3) a field width 4) a decimal position (optional) I Format code for integer input read (unit = *, fmt = “(i3, i4, 2 i6, 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 - - -__ 3 i3 0
Explicit format control f Format code for real output write (unit = *, fmt = “(t2, f10.3, f6.0, f8.2, f5.2, f6.3)”, A, B, B, C, H real variableinternal valueformat codeexternal 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
Explicit format control f Format code for real input read (unit = *, fmt = “(f10.4, f8.0, f5.3, f13.6, f9.0, f8.4, f8.2)”, A, B, C, D, E, F, G real variableinternal valuefield widthexternal appearance A -897.6577 10 _ -897.6577 B +234. 8 _ _ + _ 234. C -0.12 5 - . 12 _ D -897.6577 13 -8.976577E+02 E -0.02032 9 -20.32E-3 F +2.032 8 2032.e-3 G 2032.0 8 2.032E3_ IMPORTANT: If a decimal point appears in the input field, the value of d in the format code is ignored; the actual decimal point overrides the format specification.
Explicit format control FORMAT RESCAN If there are more list items than format codes in the format description, the input or output process must continue to another record in order to complete the list. For example; suppose that we use the format description (f12.4, i3) which specifies an input record containing only two fields. If five data items are be read, the format must be rescanned to match the remaining items of the list. In the first of the following examples, the rescan point is near the middle of the format; in the second, the rescan point is at the repeat count that immediately follows the opening right parenthesis of the format. (2 (i5, i4, i3), f5.2, 2(i5, i4, i3), f5.2, i6) ^ rescan point (2 ( 2 (i5, i4, i3), f5.2), i6) ^ rescan point
format specification • Format specification with named constant Instead of a literal character constant, a format specifier may be the name of a character string that contains the format description. program test implicit none real :: A, B, C integer :: J, K character (len = *), parameter :: F14 = “(3 f12.0, 2 i3)” read (unit = *, fmt = F14) A, B, C, J, K write (unit = *, fmt = F14) A, B, C, J, K stop end program test
Files • A file is an external source from which data may be obtained or an external destination to which it may be sent. Data can not be transferred to or from a file until the file is connected to the program.
Files CONNECTION PROPERTIES A connection between a unit and a file has certain connection properties. When a unit becomes connected to a file by execution of an open statement, the following properties may be established: 1) An access method, sequential or direct, is always established. 2) A form, formatted or unformatted, is always established. 3) An initial position, rewind or append, specifies initial positioning of the file during execution of open statement 4) A permanence property, temporary or permanent, specifies disposition of the file when the connection is terminated. 5) An action property, read, write or read-write, specifies which kinds of Data transfer statements are permitted. 6) A record length may be established.
Files, The OPEN Statement The statement consists of the keyword open followed by a control list. A control list must include the following specifiers. 1) status = Character expression, where the value of Character expression is old, new, replace, or scratch. 2) action = Character expression, where the value of Character expression is read, write, or readwrite. 3) position = Character expression, where the value of character expression is rewind or append. The followings are optional specifiers 4) access = Character expression, where the value of character expression is sequential or direct (default is sequential). 5) form = Character expression, where the value of Character expression is formatted or unformatted (default formatted). 6) recl = Integer expression, where the value of Integer expression is positive. 7) iostat = Integer variable name.
Files Here some examples for open statement: open (unit = 6, file =“My_Data.bin”, status =“old”, action =“read”,& form = “unformatted”) open (unit = J, file =“My_Input.txt”, status =“old”, action =“read”) open (unit = 31, file =“X23”, status =“old”, action =“read”, & access =“direct”, recl =720) • See program data_transfer.f
I / O positioning INPUT POSITIONING Let’s consider an existing file has been connected to unit 7 for sequential access. A rewind initial position property for the connection was specified in the open statement. Suppose that several read statements are then executed. 1) If the file is unformatted, each read will advance the file by one record. 2) If the file formatted, each advancing read statement will advance the file by one record. 3) Each non advancing read from a formatted file will advance the file when a slash is encountered in the format or when rescan occurs, and the position remains within the current record at the end of execution of the statement. Let us suppose that the end-of-file indicator is encountered. The file is then positioned following the end-of-file indicator, and indication of the end-of-file condition is returned to the program.
I / O positioning OUTPUT POSITIONING Now let’s consider a file that has to be used for sequential output. It is connected to unit 13 and contains no records, no indication of the end-of-file condition. A rewind initial position property for the connection was specified in the open statement. Suppose that several write statements are then executed. 1) If the file is unformatted, each write will add one record to the file. 2) If the file formatted, each advancing write will add at least one record. 3) A non advancing write will add a new record when a slash is encountered in the format or when rescan occurs, but will not end the record after data transfer. After the desired data has been transmitted to the file, the statement end file may be executed to append an end-of-file indicator following the last record written.
Direct access input and output The connection of a file to a unit has an access method, which is sequential or direct. Direct access means that records may be accessed in an arbitrary sequence. Each record of the file has a unique record number that is established when the record is written and does not change. A Record number specifier in the Control List of each read or write statement indicates which record is to be read or written; this specifier has the form rec = Record number Record number is an integer expression with a positive value. Example for direct access output statement: write (unit = 3, fmt = *, rec = 76) X, Y, Z, Z5 • write statement sends information to record number 76 of unit 3.
Unformatted input and output The connection of a file to a unit has a form which is formatted or unformatted. Unformatted files usually correspond to so-called binary files, whereas formatted files correspond to text files. Unformatted records contain information in a processor-dependent form that is obtained by transferring data to and from internal storage without any editing or other transformation. The external representation of the data corresponds exactly to its internal representation. Thus, unformatted data transfer is more efficient because the data is merely copied between the external device and the internal storage of the computer. Formatted records are composed of characters representing data values; a formatted record corresponds to a printed line. Conversion is required between the internal form of the data and its representation on the file.
formatted internal data transfer Formatted internal data transfer changes the representation of data without actually transmitting it to or from an external device. Formatted external data transfer includes two separate processes: a) The change in representation of data between its internal form and its external form. b) The transmission of data to or from the external device. In an internal data transfer statement, the integer expression or asterisk Unit specifier in the control list is replaced by the name of a character variable that serves as a buffer or “internal file”.
Read pages between 264-291 and 454-493 from the book by Ellis & Philips. • Do all of the self exercises and programming exercises in these sections and submit the answers to programming exercises of 10.2, 10.9 and 10.10.
Access to derived types • privateonly available without restriction in the defining module • publicfreely available without restriction throughout a program
Example type, public :: complex_number private real :: a, phi end type • Privacy applies outside the module • Within the module, the components are fully accessible
Accessing the components • Accessing: • Giving values to components • Printing values of components • Doing calculations (with components) • Write procedures and put them in the same module!
Data hiding • Allowing access to a restricted set of the entities in a module • private statement private :: internal_procedure real, private :: internal_value type, private :: internal_type real :: x, y endtype internal_type