Meaning of READ in Fortran

后端 未结 6 587
野趣味
野趣味 2020-12-09 05:02

What does READ() do in Fortran?

For example:

READ(1,82)
6条回答
  •  有刺的猬
    2020-12-09 05:55

    1 is the file handle, which you have to open with the proper open call. 82 is a label that references a format, meaning how you will report the data in terms of visual formatting.

            program foo
            implicit none
            integer :: i
            double precision :: a
    
            write (*,*) 'give me an integer and a float'
            read (*,82) i,a
            write (*,82) i,a
    82      format (I4, F8.3)
            end program
    

    In this example, the program accepts from the standard input (whose unit number is not specified, and so I put a *) an integer and a floating point value. the format says that the integer occupies the first four columns, then I have a float which stays in 8 columns, with 3 digits after the decimal point

    If I run the program now, and I don't follow exactly this format, the program will complain and crash, because the first 4 columns are expected to represent an integer (due to the I4 format), and "5 3." is not a valid integer

    $ ./a.out 
     give me an integer and a float
    5 3.5
    At line 7 of file test.f (Unit 5)
    Traceback: not available, compile with -ftrace=frame or -ftrace=full
    Fortran runtime error: Bad value during integer read
    

    However, a correct specification (please note the three spaces before the number 5) will perform the correct operation (with a little tolerance, it's not that strict)

    $ ./a.out 
     give me an integer and a float
       5 3.5
       5   3.500
    $ 
    

提交回复
热议问题