0

I've already checked a similarly existing topic (How to read numeric data from a string in FORTRAN), but I'm not being able to do what I want.

I need to open a file and read a numeric value from a string. Bellow there's a section of the file in question. I want to read the integer next to 'ELEMENTS:', but so far I'm not being able to do so.

      ELEMENT GROUP 2.4.6
GROUP:          1 ELEMENTS:     187169 MATERIAL:          2 NFLAGS:          1
                           fluid
       0
       1       2       3       4       5       6       7       8       9      10
      11      12      13      14      15      16      17      18      19      20
      21      22      23      24      25      26      27      28      29      30
      31      32      33      34      35      36      37      38      39      40

Can someone please help me here?

Community
  • 1
  • 1
André Almeida
  • 379
  • 1
  • 4
  • 11
  • You should show the code you have now, in a question like this. Or explain just what problem you have, which makes you not able to do what you want. – hyde Mar 27 '15 at 19:17
  • this is an exact duplicate of the question you reference ( just change '=' to 'ELEMENTS:' .. ) – agentp Mar 27 '15 at 20:00
  • On second thought, since you need to search for a substring you need to use `index` instead of `scan` : `index(str,"ELEMENTS:")+10` – agentp Mar 27 '15 at 20:04
  • `index` returns the position of the start of the substring, so you add the length to get to the end ( I counted wrong it should be `+9` ). This is the same purpose as the `+1` in the referenced answer. – agentp Mar 27 '15 at 21:50

1 Answers1

1

Ok guys, thanks to your answers the program is working!

For further reference, here's the reading part of the code:

READ(77,'(A)') str
ipos = INDEX(str,"ELEMENTS:",back=.true.) + 9
READ (str(1+ipos:),*) k
PRINT*, k

Thank for the answers.

André Almeida
  • 379
  • 1
  • 4
  • 11