Reading real numbers from a text file in Fortran 95

  • Context: Fortran 
  • Thread starter Thread starter Sirluke
  • Start date Start date
  • Tags Tags
    fortran
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
Sirluke
Messages
3
Reaction score
0
Hi Guys

I need some help with reading a row of real numbers on a text file using fortran. On my text file the row is:
74.05 112.91 154.03 193.90 236 276.71 61.12.
I would like to point out that these numbers are chemical parameters that can vary but they usually have up to two digits in the decimal part and three in the unit part. So if I write:

open(1,'file.txt)
.
read(1,100) vector
100 format (7(f6.2))

then i get very wrong results in reading the data, how can i fix this? Thank you very much
 
Physics news on Phys.org
Try a list-directed read. Just replace the 'read(1,100) with read(1,*). If the dimension of vector is 7, it should work. Otherwise, you need to specify that it should only try to read 7 numbers: read(1,100) vector(1), vector(2), vector(3), vector(4), vector(5), vector(6), vector(7), (There are other ways to specify the 7 variables, but this should work.)
 
Thank you very much :-) The list-directed works perfectly!