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?

4
  • 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. Commented Mar 27, 2015 at 19:17
  • this is an exact duplicate of the question you reference ( just change '=' to 'ELEMENTS:' .. ) Commented Mar 27, 2015 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 Commented Mar 27, 2015 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. Commented Mar 27, 2015 at 21:50

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.