Reading an array from a text file with Fortran 90/95
May 21st, 2010 Posted in UncategorizedIf you’re used to coding in more modern languages, Fortran I/O can seem a little bizarre. Strings in Fortran are much more difficult to work with, since they are fixed-length rather than null-terminated. The following example illustrates a simple way to read an array of numbers from a text file when the array length is unknown at compile time.
program io_test
real, dimension(:), allocatable :: x
integer :: n
open (unit=99, file='array.txt', status='old', action='read')
read(99, *), n
allocate(x(n))
read(99,*) x
write(*,*) x
endHere is the text file that the array is read from. The integer on the first line is the number of elements to read from the next line.
10 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
One Response to “Reading an array from a text file with Fortran 90/95”
By Marcel Cobián on Feb 4, 2012
good job!