Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading complicated text and digit from text file

Status
Not open for further replies.

mohammadsh

Technical User
Jan 17, 2010
1
US
I am trying to write program to just read the input of one commercial simulator and then I should try to write my own simulator.

the input of that software is in dat.file and I am trying to write a program to just read the input and allocate them in appropriate defined variable.

here is one part of input:(It shows that I have a cube cube such that nx=100,ny=1,nz=1)

DIMENS
100 1 1 /


I want to write program that when by reaching to the keyword "DIMENS" it should go to the next line and read first number as nx=100, second as ny=1 and third as nz=1
 
Hi!
I'm working on the same issue.
My solution to this problem is quite "CPU consuming", but it works. Surely there are better solutions!

You can try using the subsequent approach:

...
flag=0
100 FORMAT(format of keyword "DIMENS")
200 FORMAT(format of coordinates)
OPEN(UNIT=1,FILE="dat.file",ACTION="read",STATUS="old")
DO i=1,(number of lines in dat.file-1)
READ(1,100) keyword
IF (keyword=="DIMENS") THEN
flag=i
END IF
IF ((flag/=0).AND.(i==(flag+1)) THEN
READ(1,200) nx,ny,nz
END IF
ENDDO
CLOSE(UNIT=1)
...
now you can use nx, ny and nz as you wont!

Please note that you MUST define variables and be sure that the relationschip between number of lines is correct. Sorry, this is only logical scheme. You have to write and complete code.
Let me know if it works.
bye bye
 
Just read a line of text. To do so, declare:

CHARACTER(LEN=80) :: line

and then read...

READ(1,'(A80)')line
i=INDEX(line,'DIMENS')
IF(i/=0)THEN... ...and so on.

i will be zero if 'DIMENS' doesn't occur in the line
 
....it's true!

it was my fault: maybe in the line there are a lot of characters.

Thenk you GerriGroot!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top