![]() |
||||||||
![]() |
![]() |
![]() |
![]() |
|||||
![]() |
||||||||
![]() |
||||||||
![]() |
![]() |
|||||||
![]() |
||||||||
![]() |
||||||||
![]() |
||||||||
![]() |
||||||||
![]() |
||||||||
FORTRAN 90+: FREE FIELD READ AND WRITE
The READ and WRITE commands are used to read write data from and to the command prompt. The free field is the simplest way to read and write data.
WRITE
The WRITE command is used to provide a final solution that can be read on the screen. The command is initiated with WRITE and is then followed by the information to be printed on the screen where each individual piece of information is separated by a comma. What follows in a simple example of the use of WRITE.
PROGRAM Name
IMPLICIT NONE
REAL :: height
INTEGER :: age
CHARACTER(LEN=12) :: school
height = 6.20
age = 21
school = ’Georgia Tech’
WRITE(*,*) "Mike’s age is",age,". He goes to ", school,".&
&He is",height,"feet tall."
END PROGRAM
The above code produces the string:
Mike’s age is 21. He goes to Georgia Tech. He is 6.2 feet tall.
READ
The READ function is used to bring a value into the program from the command prompt entered by the user. The command is initiated in the code by typing READ(*,*). It is then followed by one or more variable names that will correspond to the input values, separated by commas. The type of each variable must be followed when the values are entered in the command prompt (a REAL variable has a decimal point while an integer does not). The following is an example code where both READ and WRITE are used.
PROGRAM Name
IMPLICIT NONE
REAL :: height
INTEGER :: age
CHARACTER(LEN=20) :: your_name, answer
WRITE(*,*) "Please, tell me your name, age and height"
WRITE(*,*) " using the data types CHARACTER INTEGER "
WRITE(*,*) "and REAL."
READ(*,*) your_name, age, height
WRITE(*,*) "Type ’y’ if you name is ",your_name
WRITE(*,*) "your age is ", age," and your height is"
WRITE(*,*) height, ". Type’n’ otherwise."
READ(*,*) answer
IF (answer .EQ. ’y’) THEN
WRITE(*,*) "Thanks ", your_name
ELSE
WRITE(*,*) "Sorry, there has been an error come"
WRITE(*,*) "back later."
END IF
END PROGRAM
The code will ask:
Please, tell me your name, age and height using the data types CHARACTER INTEGER and REAL.
With and input of, Ben 20 6.2 or Ben,20,6.2 the output would be
Type ’y’ if you name is Ben your age is 20 and your height is 6.2.
Input ’y’
Output Thanks Ben
The above code tells the user to enter the correct type of data because entering an unexpected data type into the READ request will result in an error. This is important as any incorrectly formatted information input into a READ will result in an error.













