How to Read From a File in Fortran

FORTRAN (FORmula TRANslator) is a programming language that efficiently handles formula calculations, but does not support an object-oriented style. FORTAN is primarily used to write scientific programs. Reading from files is necessary to access a large amount of data within the program. FORTRAN allows you to read files using the sequential access -- line by line.

Instructions

    • 1

      Open the file using the command:

      open (unit = 20, file = "data.txt")

      Note that a unit, "20" in this example, is a number that allows you to refer to the file within the program.

    • 2

      Read data from the file using the "read" statement such as

      100 read(20,*, end=200) var1, var2

      The data are assigned to the variables "var1" and "var2", if for example, the file has two columns of numbers. The labels "100" and "200" are necessary to organize the loop to access every line in the file.

    • 3

      Add the following two commands to complete the loop:

      goto 100

      200 continue

      The operator "goto" redirects the program to read the next line from the file; when the end of the file is reached the program goes to outside the loop ( the label "200").

    • 4

      Close the file using the the following statement:

      close (20)

Related Searches:

References

Comments

Related Ads

Featured