Question

I am facing problem doing loop in a loop. I don't know why loop in a loop is not running.

Program Console2
IMPLICIT NONE

Integer n,LineID,AA,AAA,BBB,CCC,DDD,EEE,FFF,GGG,J
Real :: A,B,C,D,S,BB,CC,DD
Character (LEN=10) Line,NodeID
OPEN(UNIT=2,FILE="Chaq.txt",STATUS='unknown')
OPEN(UNIT=12,FILE="CoOrd.txt",STATUS='unknown')
OPEN(UNIT=4,FILE="OUTPUT.txt",STATUS='unknown')


Do n=1,10
  Read (2,'(1x,a6,5x,i5,7x,i4,i8,i8,i8,i8)')NodeID,AAA,BBB,CCC,DDD,EEE,FFF

  write (4,*)NodeID,DDD

  Do J=1,10

    READ (12,('(1x,a4,7x,I5,8x,f8.2,f8.2,f8.2)'))Line,AA,BB,CC,DD

    write (4,*) Line,AA

  End Do

End Do

End  program Console2

Problem is for second loop it is not reading the values from second file: OUTPUT File:

 CQUAD4            5950
 GRID              3860
 GRID              3861
 GRID              3862
 GRID              3863
 GRID              3864
 GRID              3865
 GRID              3866
 GRID              3867
 GRID              3868
 GRID              3869
 CQUAD4            5949

Files Chaq.text: some lines

 CQUAD4      6817       2    6053    5950    5951    6054                   
 CQUAD4      6816       2    6052    5949    5950    6053                   
 CQUAD4      6815       2    6051    5948    5949    6052                   
 CQUAD4      6814       2    6050    5947    5948    6051            
 CQUAD4      6813       2    6192    5946    5947    6050

CoOrd.txt : Some Lines I shown

 GRID        3860        -171.90469.00543-28.5831                           
 GRID        3861        -186.50872.15994-28.5831                           
 GRID        3862        -180.23572.08246-28.5831                           
 GRID        3863        -184.573103.9741-28.5831                           
 GRID        3864        -171.92971.30236-28.5831                           
 GRID        3865        -191.05479.44247-28.5831                           
 GRID        3866        -159.938100.6297-28.5831                           
 GRID        3867        -169.58477.11039-28.5831                           
 GRID        3868        -176.62391.22597-28.5831                           
 GRID        3869        -185.83898.214  -28.5831     
Était-ce utile?

La solution

If you are wanting to read from the beginning of CoOrd.txt each time through the outer loop, then you need to rewind CoOrd.txt each time through the loop:

    Integer ReadStat
    ...

    Do n=1,10
      Read (2, '(1x,a6,5x,i5,7x,i4,i8,i8,i8,i8)', IOSTAT=ReadStat) NodeID,AAA,BBB,CCC,DDD,EEE,FFF
      If (ReadStat < 0) exit   ! EOF reached
      If (ReadStat > 0) stop   ! An unknown error occurred

      write (4,*)NodeID,DDD
      rewind(12)

      Do J=1,10
        READ (12, ('(1x,a4,7x,I5,8x,f8.2,f8.2,f8.2)', IOSTAT=ReadStat))Line,AA,BB,CC,DD

        If (ReadStat < 0) exit   ! EOF reached
        If (ReadStat > 0) stop   ! An unknown error occurred

        write (4,*) Line,AA

      End Do

    End Do
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top