Problem with file input within nested loops 
Author Message
 Problem with file input within nested loops

I have the following program:
      PROGRAM read_matrix
         IMPLICIT NONE
         INTEGER i, j
         REAL*8 matrix(4, 4)

         OPEN(8, FILE='matrix.txt', STATUS='old', FORM='formatted')
         DO i = 1, 4
            READ(8, *) (matrix(i, j), j = 1, 4)
         ENDDO
! Just an example:
         WRITE(*, *) 'Matrix:'
         DO i = 1, 4
            WRITE(*, *) (matrix(i, j), j = 1, 4)
         ENDDO
         STOP
      END
And it works (matrix.txt contains a 4x4 matrix, of course). Now if I make
it:
      PROGRAM read_matrix
         IMPLICIT NONE
         INTEGER i, j
         REAL*8 matrix(4, 4)

         OPEN(8, FILE='matrix.txt', STATUS='old', FORM='formatted')
         DO i = 1, 4
            DO j = 1, 4
               READ(8, *) matrix(i, j)
            ENDDO
         ENDDO
         WRITE(*, *) 'Matrix:'
         DO i = 1, 4
            DO j = 1, 4
               WRITE(*, *) matrix(i, j)
            ENDDO
         ENDDO
         STOP
      END
I get an end-of-file error. I didn't modify matrix.txt meanwhile. What is
wrong with the second program?

--
Armando di Matteo <a r m y one nine eight seven at e m a i l dot i t>
http://www.*-*-*.com/
[Insert beautiful quotation here]



Tue, 31 Aug 2010 23:10:14 GMT  
 Problem with file input within nested loops

Quote:
> I have the following program:
> ? ? ? PROGRAM read_matrix
> ? ? ? ? ?IMPLICIT NONE
> ? ? ? ? ?INTEGER i, j
> ? ? ? ? ?REAL*8 matrix(4, 4)

> ? ? ? ? ?OPEN(8, FILE='matrix.txt', STATUS='old', FORM='formatted')
> ? ? ? ? ?DO i = 1, 4
> ? ? ? ? ? ? READ(8, *) (matrix(i, j), j = 1, 4)
> ? ? ? ? ?ENDDO
> ! Just an example:
> ? ? ? ? ?WRITE(*, *) 'Matrix:'
> ? ? ? ? ?DO i = 1, 4
> ? ? ? ? ? ? WRITE(*, *) (matrix(i, j), j = 1, 4)
> ? ? ? ? ?ENDDO
> ? ? ? ? ?STOP
> ? ? ? END
> And it works (matrix.txt contains a 4x4 matrix, of course). Now if I make
> it:
> ? ? ? PROGRAM read_matrix
> ? ? ? ? ?IMPLICIT NONE
> ? ? ? ? ?INTEGER i, j
> ? ? ? ? ?REAL*8 matrix(4, 4)

> ? ? ? ? ?OPEN(8, FILE='matrix.txt', STATUS='old', FORM='formatted')
> ? ? ? ? ?DO i = 1, 4
> ? ? ? ? ? ? DO j = 1, 4
> ? ? ? ? ? ? ? ?READ(8, *) matrix(i, j)
> ? ? ? ? ? ? ENDDO
> ? ? ? ? ?ENDDO
> ? ? ? ? ?WRITE(*, *) 'Matrix:'
> ? ? ? ? ?DO i = 1, 4
> ? ? ? ? ? ? DO j = 1, 4
> ? ? ? ? ? ? ? ?WRITE(*, *) matrix(i, j)
> ? ? ? ? ? ? ENDDO
> ? ? ? ? ?ENDDO
> ? ? ? ? ?STOP
> ? ? ? END
> I get an end-of-file error. I didn't modify matrix.txt meanwhile. What is
> wrong with the second program?

> --
> Armando di Matteo <a r m y one nine eight seven at e m a i l dot i t>http://army1987.890m.org/
> [Insert beautiful quotation here]

Each read in the two programs consumes an entire line. So, in the
first
case you read 4 lines and that is exactly what is in the file. In the
second case you try to read 16 lines (one number per read). And that
does not work.

Regards,

Arjen



Tue, 31 Aug 2010 23:14:34 GMT  
 Problem with file input within nested loops

Quote:

> Each read in the two programs consumes an entire line. So, in the
> first
> case you read 4 lines and that is exactly what is in the file. In the
> second case you try to read 16 lines (one number per read). And that
> does not work.

I get it now: one read statement, one line, much like with write
statements, right?

--
Armando di Matteo <a r m y one nine eight seven at e m a i l dot i t>
http://army1987.890m.org/
[Insert beautiful quotation here]



Wed, 01 Sep 2010 04:00:46 GMT  
 Problem with file input within nested loops

Quote:


>>Each read in the two programs consumes an entire line.
>> So, in the first case you read 4 lines and that is exactly

 >>what is in the file. In the second case you try to read 16
 >> lines (one number per read). And that does not work.

Quote:
> I get it now: one read statement, one line, much like

 > with write statements, right?

Well, one or more whole lines in both cases.

If you have four values per line and read in five with
one READ statement, it will use two lines and ignore the
rest on the second line.

-- glen



Wed, 01 Sep 2010 04:48:09 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. 2 files: a loop within a loop??

2. nested while loops, inside loop not stopping correctly

3. problem with nested while-loops

4. nested loop problem

5. Replacing a string from an input file within awk script

6. How to specify input file within awk script?

7. using while loops within while loops?

8. Problem with consecutive running of a gawk script within a loop

9. Looping through input file, only access even lines.

10. That nested nested nested if statement...

11. Nested Control Structures Within Iterators

12. how to nest while loops

 

 
Powered by phpBB® Forum Software