Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Read & Write statements

A file is a sequence of records that itself is a sequence of values or characters. A FORTRAN program can read from and write to the files stored on an external storage device. Instead of the usual READ statement, file processing employs a more general form of READ statement. Also, WRITE command replaces the PRINT command.

READ (cilist)  input_list
WRITE (cilist) output_list

cilist is a list of specifiers pointing to the file, its format, and other processing details with commas in between. Some of the specifiers are UNIT, FMT, ERR, and END. In cilist, the order of the specifiers does not matter. The value of the UNIT specifier gives a unit identifier specific to a file. For instance, each file is associated with a unit number, which is an integer between 1 to 99. Integers 5 & 6 specifies standard input & output respectively. Asterisk (*) refers to the keyboard for input and screen for the output. The value of the FORMAT specifier FMT is the label of FORMAT statement.

READ (UNIT = 5,  FMT = 100) X, Y, Z
or
READ (FMT = 100,  UNIT = 5) X, Y, Z
(order of the specifiers does not matter)

If one requires reading the content of the file as it is i.e without any formatting, write unit specifier in the first place without the keyword UNIT and in place of FORMAT specifier place an asterisk (*) symbol.

READ ( 5, *)  X,  Y,  Z
Image depicting the use of READ and WRITE commands along with UNIT and FMT specifiers for file processing in FORTRAN 77
Image depicting the use of READ and WRITE commands along with UNIT and FMT specifiers for file processing in FORTRAN 77

Now, if any error occurs during the execution of the program, it stops immediately. If an error specifier of the form ERR=label is present, it continues to run from the statement with the tag label. Hence dealing with errors becomes easier. Similarly, specifier END=label prevents from input errors. If the READ statements attempt to read a record that is not in the file, an error will occur. Using an END specifier in the READ command triggers the End-of-file condition when the input is complete. Thereby it prevents the user from such an input error in the program.

Image showing the application of END specifier in FORTRAN 77 program
Image showing the application of END specifier in FORTRAN 77 program

Open/Create the file

A unit identifier is assigned to the files other than the standard I/O files. An OPEN statement calls the file with its name and labels it with a unit identifier. 

OPEN (open_list)

open_list is a list of specifiers of the form keyword= value with commas in between. It includes two specifiers: UNIT and FILE. The value of the UNIT is the identifier of the file and the value of FILE is the name of the file. If no such file exists, a new file appears in York directory with this name . For example:

OPEN ( UNIT = 8, FILE = 'MYFILE.DAT' )
Image showing the application of OPEN command for file processing in FORTRAN 77
Image showing the application of OPEN command for file processing in FORTRAN 77

Is file processing using FORTRAN77, Some other specifiers such as STATUS, ACCESS, FORM, RECL, BLANK, ERR and IOSTAT also appear with OPEN command. These are optional and not very important to the program.

Close the file

Close command closes a single or several files simultaneously. For example: First statement below close a single file with unit number 5. The second command three files simultaneously. However, it is not necessary to close the files using CLOSE command. STOP or END statements close them automatically. 

close (5)
close (1,2,3)
Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *


Translate »
error

Enjoy this blog? Please encourage us by following on

Instagram