-1

When I write the following .dat file, the file is only 6397 lines long even though the nested do loops correctly iterate 6400 times. Also, when I output the data NX(BR,BC) to Terminal, everything is fine. Only the .dat file is missing a few lines. Any idea what the issue might be?

Note NX is defined as REAL(8),DIMENSION(0:2000,0:2000) :: NX

Here's the rest of the relevant part of the code:

OPEN(UNIT=18,FILE='test_file.dat',STATUS='UNKNOWN',ACTION='WRITE')
I = 0 ! for debugging

DO BC = 0,39
   DO BR = 0,159
     ! do some calculations here to calculate NX(BR,BC)

     ! for debugging
     I = I + 1

     WRITE(18,*) NX(BR,BC)
  ENDDO
ENDDO

WRITE(*,*) I ! outputs 6400
veryreverie
  • 2,871
  • 2
  • 13
  • 26
user2561523
  • 115
  • 5

1 Answers1

2

I tested your code with my raspberry pi 4 (GNU Fortran (GCC) 11.2.0) and got exactly 6400 lines of zeros.

Maybe the write buffer is not flushed correctly. Have you tried closing and re-opening the file: How to carriage return and flush in Fortran?

close(18)
Threnloe
  • 66
  • 1
  • 3
  • 10
  • Wow, that did it! Why is the close statement necessary? – user2561523 Apr 12 '22 at 19:40
  • 1
    @user2561523, the CLOSE statement isn't necessary, a FLUSH statement would also be appropriate. Indeed, if `close(18)` works for you then this shows the importance of a [mre]: closing is implied by normal termination of a complete program. Without a full example we can only guess why closing helps in your case. (If you do close the connection, instead of flushing, you will have to pay attention to what that means.) – francescalus Apr 12 '22 at 19:58
  • 1
    The original question was spesifically for Fortan 90, which does not have flush in standard library (this came in 2003). – Threnloe Apr 12 '22 at 20:23