tags:

views:

583

answers:

3
+1  Q: 

FORTRAN READ()

Excuse me if this is a duplicate question, but I didn't see anything exactly like this.

What does READ() do in FORTRAN?

For example:

READ(1,82)
+2  A: 

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
+5  A: 

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
Thank you, Alex.
KG
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
@David, heh yes, brings me back to my youth!-)
Alex Martelli
@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
A: 

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

anon
and how's your answer adds to this thread?
SilentGhost