0
votes

I am having trouble reading exponential from a text file using Fortran.

The entry in the text file looks like the following

  0.02547163e+06-0.04601176e+01 0.02500000e+02 0.00000000e+00 0.00000000e+00    3

And the code that I am using looks like the following

   read(iunit,'(ES20.8,ES20.8,ES20.8,ES20.8,ES20.8,I2)') dummy1, dummy2, Thermo_DB_Coeffs_LowT(iS,1:3),temp 

The error I am getting is

Fortran runtime error: Bad value during floating point read

How can I read these values?

1
Your field widths for the reals are 20, but that doesn't suit the input. Can you check that the input is represented correctly here? - francescalus
I'd use some text processing utility (eg sed) to put spaces into the file where f-p values have run together, then read with list-directed input. - High Performance Mark
try 15.8. also I don't think S does anything on input, so just E15.8. And go have a talk with whoever created such file with numbers run together with no delimiters - agentp
As @agentp says, input with ES is treated just like input with E. This is further treated like F. Also, you should prefer not to use F15.8 but F15.0 (or whatever field width). - francescalus
In general formatted reading should be avoided - it offers nothing and can only cause trouble, depending on the data to be read. I agree with HPF, the data file needs to be modified (by sed or a macro in an editor), so that it will separate numbers with spaces (one space would be enough, but you can add more, it doesn't really matter). Then read(iunit,*) will just do the job perfectly. While I always use formatted printing, I really can't see any reason to use formatted reading, other than looking for potential trouble. - Pap

1 Answers

0
votes

Well here is what I usually do when it is too painful to hand edit the file...

CHARACTER(LEN=256)   :: Line
INTEGER, PARAMETER   :: Start = 1
INTEGER              :: Fin, Trailing_Int, I
DOUBLE, DIMENSION(6) :: Element
...

Ingest_All_Rows: DO WHILE(.TRUE.)
  READ(...) Line  ! Into a character-string
  <if we get to the end of the file, then 'EXIT Ingest_All_Rows'>

  Start =1
  Single_Row_Ingest: DO I = 1, 6
    Fin = SCAN(Line,'eE')+3  !or maybe it is 4?
    IF(I ==6) Fin = LEN_TRIM(Line)
    READ(Line(Start:Fin),*) Element(I)   !fron the string(len-string) to the double.
    Line = Line((Fin+1):)
    IF(I ==6) Trailing_Int = Element(6)
  ENDDO Single_Row_Ingest

  <Here we shove the row's 5 elements into some array, and the trailing int somewhere>
ENDDO Ingest_All_Rows

You will have to fill in the blanks, but I find that SCAN and LEN_TRIM can be useful in these cases