Fortran reading mixed string and numeric data

I am having problems with the read statement. I would prefer Fortran90, although other versions may be possible if this helps. There are several lines of data in the file that can be described as:

  • space separated
  • not in fixed format
  • containing a string followed by 7 numbers String
  • contains a slash /

Here is an example:

maxele/OS1_HC_LF_A_0001_004_maxele.63 4.22E-03 9.00E-01 1.00E-06 1 -999 -999 -999 maxele/OS1_Inl_A_0001_005_maxele.63 2.11E-03 9.00E-01 1.00E-06 3 -999 -999 -999 maxele/OS1_HC_LF_C_0001_009_maxele.63 1.56E-03 9.00E-01 1.00E-06 2 58.77 -82.82 28.91 maxele/OS1_TS_B_0001_006_maxele.63 3.90E-03 9.00E-01 1.00E-06 1 -999 -999 -999 

I found out that Fortran will stop the read statement if it encounters a slash character (/) for unformatted reads, so I have to use format specifiers. And since the length of the string is unknown, I'm not sure how to make sure that the reading of the string stops in the first space. I believe that you can read along the line, then analyze later, but this seems confusing. Is there no way to make it treat the data as separated by spaces? Thank you in advance.

+3
source share
1 answer

you need to read the entire line and parse, in this case it is not so bad, because you only need to parse the first line and read the internal list of the rest.

 read(unit,'(a)')string !declared long enough for a whole line iblnk=index(string,' ') read(string(iblnk:),*)seven_reals 
+5
source

Source: https://habr.com/ru/post/970427/


All Articles