For Programmers: Free Programming Magazines  


Home > Archive > Fortran > March 2008 > Problem with file input within nested loops









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem with file input within nested loops
Army1987

2008-03-14, 7:32 pm

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]
Arjen Markus

2008-03-14, 7:32 pm

On 14 mrt, 16:10, Army1987 <army1...@NOSPAM.it> wrote:
> I have the following program:
> =A0 =A0 =A0 PROGRAM read_matrix
> =A0 =A0 =A0 =A0 =A0IMPLICIT NONE
> =A0 =A0 =A0 =A0 =A0INTEGER i, j
> =A0 =A0 =A0 =A0 =A0REAL*8 matrix(4, 4)
>
> =A0 =A0 =A0 =A0 =A0OPEN(8, FILE=3D'matrix.txt', STATUS=3D'old', FORM=3D'fo=

rmatted')
> =A0 =A0 =A0 =A0 =A0DO i =3D 1, 4
> =A0 =A0 =A0 =A0 =A0 =A0 READ(8, *) (matrix(i, j), j =3D 1, 4)
> =A0 =A0 =A0 =A0 =A0ENDDO
> ! Just an example:
> =A0 =A0 =A0 =A0 =A0WRITE(*, *) 'Matrix:'
> =A0 =A0 =A0 =A0 =A0DO i =3D 1, 4
> =A0 =A0 =A0 =A0 =A0 =A0 WRITE(*, *) (matrix(i, j), j =3D 1, 4)
> =A0 =A0 =A0 =A0 =A0ENDDO
> =A0 =A0 =A0 =A0 =A0STOP
> =A0 =A0 =A0 END
> And it works (matrix.txt contains a 4x4 matrix, of course). Now if I make
> it:
> =A0 =A0 =A0 PROGRAM read_matrix
> =A0 =A0 =A0 =A0 =A0IMPLICIT NONE
> =A0 =A0 =A0 =A0 =A0INTEGER i, j
> =A0 =A0 =A0 =A0 =A0REAL*8 matrix(4, 4)
>
> =A0 =A0 =A0 =A0 =A0OPEN(8, FILE=3D'matrix.txt', STATUS=3D'old', FORM=3D'fo=

rmatted')
> =A0 =A0 =A0 =A0 =A0DO i =3D 1, 4
> =A0 =A0 =A0 =A0 =A0 =A0 DO j =3D 1, 4
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0READ(8, *) matrix(i, j)
> =A0 =A0 =A0 =A0 =A0 =A0 ENDDO
> =A0 =A0 =A0 =A0 =A0ENDDO
> =A0 =A0 =A0 =A0 =A0WRITE(*, *) 'Matrix:'
> =A0 =A0 =A0 =A0 =A0DO i =3D 1, 4
> =A0 =A0 =A0 =A0 =A0 =A0 DO j =3D 1, 4
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0WRITE(*, *) matrix(i, j)
> =A0 =A0 =A0 =A0 =A0 =A0 ENDDO
> =A0 =A0 =A0 =A0 =A0ENDDO
> =A0 =A0 =A0 =A0 =A0STOP
> =A0 =A0 =A0 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
Army1987

2008-03-14, 7:32 pm

Arjen Markus wrote:
> 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]
glen herrmannsfeldt

2008-03-14, 7:32 pm

Army1987 wrote:

> Arjen Markus wrote:


[color=darkred]
> 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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com