Handling End-Of-File_ the READ Statement Revisited

download Handling End-Of-File_ the READ Statement Revisited

of 2

Transcript of Handling End-Of-File_ the READ Statement Revisited

  • 8/12/2019 Handling End-Of-File_ the READ Statement Revisited

    1/2

    21/04/13 Handling End-of-File: the READ Statement Revisited

    www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/iostatus.html 1/2

    Handling End-of-File: the READ Statement

    Revisited

    In many situations, you really do not know the number of items in the input. It could be so large to be counted

    accurately. Consequently, we need a method to handle this type of input. In fact, you have encountered such atechnique in Programming Assignment 1 in which a keyword IOSTAT=was used in a READstatement. The

    following is its syntax:

    INTEGER :: IOstatus

    READ(*,*,IOSTAT=IOstatus) var1, var2, ..., varn

    The third component of the above READis IOSTAT=followed by an INTEGERvariable. The meaning of this

    new form of READis simple:

    After executing the above READstatement, the Fortran compiler will put an integer value into the integer variablefollowing IOSTAT=, IOstatusabove. Based on the value of IOstatus , we have three different situations:

    1. If the value of IOstatusis zero, the previous READwas executed flawlessly and all variables have received

    their input values. This is the normal case.

    2. If the value of IOstatusis positive, the previous READhas encountered some problem. In general, without

    knowing the system dependent information, it is impossible to determine what the problem was. However, if

    hardware and I/O devices are working, a commonly seen problem would be illegal data. For example,

    supplying a real number to an integer variable. If IOstatus is positive, you cannot trust the values of the

    variables in the READstatement; they could all contain garbage values, or some of them are fine while the

    others are garbage.3. If the value of IOstatusis negative, it means the end of the input has reached. Under this circumstance,

    some or all of the variables in the READmay not receive input values.

    What is the end of file? How do we generate it? If you prepare your input using a file, when you save it, the

    system will generate a special mark, called end-of-filemark, at the end of that file. Therefore, when you read the

    file and encounter that special end-of-file mark, the system would know there is no input data after this mark. If you

    try to read passing this mark, it is considered as an error.

    If you prepare the input using keyboard, hiting the Ctrl-Dkey would generate the end-of-mark under UNIX. Once

    you hit Ctrl-D, the system would consider your input stop at there. If your program tries to read passing this point,this is an error.

    However, with IOSTAT=, you can catch this end-of-file mark and do something about it. A commonly seen

    application is that let the program to count the number of data items as will be shown in examples below.

    Examples

    In the following code, the DO-loop keeps reading in three integer values into variables a, band c. After

    executing a READ, if Reasonis greater than zero, something was wrong in the input; if Reasonis less than

    zero, end-of-file has reached. Only if Reasonis zero, one can start normal processing.

    INTEGER :: Reason

    INTEGER :: a, b, c

  • 8/12/2019 Handling End-Of-File_ the READ Statement Revisited

    2/2

    21/04/13 Handling End-of-File: the READ Statement Revisited

    www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/iostatus.html 2/2

    DO

    READ(*,*,IOSTAT=Reason) a, b, c

    IF (Reason > 0) THEN

    ... something wrong ...

    ELSE IF (Reason < 0) THEN

    ... end of file reached ...

    ELSE

    ... do normal stuff ...

    END IF

    END DO

    The following code keeps reading an integer at a time and adds them to a variable sum. If iois greater than

    zero, it displays 'Check input. Something was wrong'; if iois less than zero, it displays the value of sum.

    Note that both cases EXITthe DO-loop since continuing the loop execution makes no sense. Otherwise,

    the value of xis meaningful and is added to sum.

    INTEGER :: io, x, sum

    sum = 0

    DO

    READ(*,*,IOSTAT=io) x

    IF (io > 0) THEN

    WRITE(*,*) 'Check input. Something was wrong'

    EXIT

    ELSE IF (io < 0) THEN

    WRITE(*,*) 'The total is ', sum

    EXIT

    ELSE

    sum = sum + x

    END IF

    END DO

    Now if the input is

    1

    3

    4

    the above code should display 8 (=1+3+4). If the input is

    1

    @

    3

    since @ is not a legal integer, the second time the READis executed, iowould receive a positive numberand the above program exits the DO-loop.