15

What does READ() do in Fortran?

For example:

READ(1,82)
francescalus
  • 30,576
  • 16
  • 61
  • 96
karlgrz
  • 14,485
  • 12
  • 47
  • 58

6 Answers6

18

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
$ 
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
6

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.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 2
    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. – David Thornley Jul 14 '09 at 15:27
  • @David, heh yes, brings me back to my youth!-) – Alex Martelli Jul 14 '09 at 15:33
  • @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. – azheglov Jul 14 '09 at 15:43
4

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!

happy coder
  • 1,517
  • 1
  • 14
  • 29
1

"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.
Syed Moez
  • 129
  • 15
0

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

0

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.

Zeus
  • 1,485
  • 1
  • 17
  • 33
  • 2
    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. – francescalus May 26 '15 at 14:17
  • I have some code where one uses `6` for screen output. What are the details exactly? – Zeus May 26 '15 at 14:51
  • 6 (pre-connected) as standard output is common, but it's not required. That is, it's a non-portable implementation detail. – francescalus May 26 '15 at 15:00
  • Zeus you've been coming here long enough that you should start familiarising yourself with the contents of the Fortran standards -- https://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 May 26 '15 at 15:52
  • 1
    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. – High Performance Mark May 26 '15 at 15:54
  • I had the assumption that `*` for `READ` functions refers to number 5 which is the `stdin`. Isn't it the case?! – Foad S. Farimani Dec 06 '18 at 22:26