Meaning of READ in Fortran

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

What does READ() do in Fortran?

For example:

READ(1,82)
相关标签:
6条回答
  • 2020-12-09 05:39

    It reads from "unit" (opened file) number 1, according to the FORMAT statement at label 82. However since the statement doesn't list any variables it has no place to put the data it's reading, which is unlikely to help; READ(1,82) FOOBAR would more usefully put the data it's reading in variable FOOBAR.

    0 讨论(0)
  • 2020-12-09 05:40

    It reads from unit 1 using the format specified by the statement numbered 82.

    0 讨论(0)
  • 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
    $ 
    
    0 讨论(0)
  • 2020-12-09 05:58

    You can do a few more things with the fortran "read" statement.

    consider: read (unit #, format, options) ... generic

    read (7,*,end=10)
    

    Where, "7" is the unit number read from, "*" is the format (default in this case), and "10" is the line number that the program control jumps to when the read device / file reaches the eof. The "options" slot can be filled with such things as "err='line number to go to'", or iostat, advance="no". You can check out some more of these

    The format part is where you can specify more precisely the format of the data that you expect. For instance, if you have a format specifier like:

    read (25,"(2X, 2I5, F7.3, A)")
    

    Here, the "2X", refers to 2 spaces, the "2I5", refers to 2 integers that are 5 digits, "F7.3", refers to a decimal value which has a total length of 7, with three digits after the decimal. The "A" refers to a character. You can check out some more of these

    CHEERS!

    0 讨论(0)
  • 2020-12-09 05:59

    When Fortran reads from a file, it required that the READ statements uniquely identify the file. The identification is done using the Fortran unit identifier.

    A unit identifier can be one of the following:

    1) An integer variable or expression whose value is greater than or equal to 0.

    2) An asterisk (*) is allowed only on READ and WRITE statements. On READ statements, an asterisk refers to unit 100 (standard input).

    Unit numbers are best supplied using newunit

    open(newunit=i,file='test')
    

    Use the INQUIRE statement to check the validity (existence) of any unit number prior to using it, as in the following example:

    logical :: idok, fop 
    inquire (unit=i, exist=idok, opened=fop)
    if (idok .and. .not. fop) then
      open (unit = i, ...)
    endif
    

    Then we have the FORMAT statement, a labelled statement which can appear in any portion of the program in which the format is visible.

    It is of the form

    READ(*,100) I, J, K
    

    The FORMAT statement

    100 FORMAT(I10,I10,I10)
    

    A slightly different FORMAT statement is

    100 FORMAT(3I10.8)
    

    which again yields three right-justified INTEGERs of width 10 but this time requires a minimum of 8 digits to be printed.

    0 讨论(0)
  • 2020-12-09 06:06

    "1" the unit that you used to open a file in fortran and "82" specifies the format for the read command.

    open(1,file=fname,status='unknown')
    read(1,82) var_name
    82 format(2I5)
    

    The code above opens a file called "fname" the read command reads from the file fname as it was opened with a unit "1" and the read command reads in the format as specified by format 82. Details on formatting in fortran is given below:

    nim (Integer Specification)
    nfm.d (Floating point Specification)
    nEm.d(Exponential Specification)
    nAm (string specification)
    
    where
    "m" is the number of character spaces reserved for printing. (should be more than what you are reading otherwise read statement would not give correct results)
    "n" is the number of integers, floating point, characters or exponential numbers that you want to read.
    "d" are the number of decimal places up to which you want to read.
    
    0 讨论(0)
提交回复
热议问题