Meaning of READ in Fortran

27,067

Solution 1

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
$ 

Solution 2

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.

Solution 3

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!

Solution 4

"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.
Share:
27,067

Related videos on Youtube

Tofystedeth
Author by

Tofystedeth

Software Engineer for Mediafly. Music is my passion outside of development. I play guitar in a couple rock bands & have been putting together a "solo" album of sorts with the recent acquisitions of a 5 string bass & a 49 key USB MIDI Controller / Keyboard (which, when combined with Battery, Cubase, & Guitar Rig, form a fairly solid home studio).

Updated on July 09, 2022

Comments

  • Tofystedeth
    Tofystedeth almost 2 years

    What does READ() do in Fortran?

    For example:

    READ(1,82)
    
    • stevedbrown
      stevedbrown almost 15 years
      I'm not sure that "fortran" and "beginner" should ever be combined.
  • David Thornley
    David Thornley almost 15 years
    IIRC, file number 5 was the equivalent of stdin, and number 6 was the equivalent of stdout. File 1 is probably a file in the file system. I seem to remember that the numbers were intended to map onto tape drives, but that was a long, long time ago.
  • Alex Martelli
    Alex Martelli almost 15 years
    @David, heh yes, brings me back to my youth!-)
  • azheglov
    azheglov almost 15 years
    @David, @Alex, this was the first programming language I learned! 5 and 6 were indeed the standard input and output, but you had to set other numbers from the command line. And you could set them to anything: disk, tape drive, virtual puncher, etc.
  • francescalus
    francescalus almost 9 years
    A few mistakes: the unit identifier may be negative (but not -1) and will be if it refers to a connection made with newunit specifier; * does not mean 100; 100--102 are possibly allowed; units 0, 5 and 6 are not certainly associated with anything; while a format statement can appear anywhere, it must be in the same inclusive scope of the data transfer statements referencing it.
  • Zeus
    Zeus almost 9 years
    I have some code where one uses 6 for screen output. What are the details exactly?
  • francescalus
    francescalus almost 9 years
    6 (pre-connected) as standard output is common, but it's not required. That is, it's a non-portable implementation detail.
  • High Performance Mark
    High Performance Mark almost 9 years
    Zeus you've been coming here long enough that you should start familiarising yourself with the contents of the Fortran standards -- gcc.gnu.org/wiki/GFortranStandards is a good place to find them (or, rather, very late drafts which most of us, disinclined to pay for published standards, are happy to use and to quote from). Intimate familiarity with the standards will ensure that your future answers are correct in all details. This one's close, but you get no cigar because of the minor errors, such as those @francescalus has pointed out.
  • High Performance Mark
    High Performance Mark almost 9 years
    To continue my previous comment: Zeus, if you're going to engage in adding answers to old Fortran questions (and there's no reason you shouldn't) you should get them right to the last dotted-i and crossed-t. OK, so that's the only reason for holding off answering old questions, not getting the answers right. But do keep working at them.
  • SRG
    SRG over 6 years
    Thanks mate! I was looking around for exactly this explanation.
  • Foad S. Farimani
    Foad S. Farimani over 5 years
    I had the assumption that * for READ functions refers to number 5 which is the stdin. Isn't it the case?!

Related