0

I've always wondered about whether or not you can carriage return and flush a write statement in Fortran. In Python/C++ you can '/r' then flush the stream.

How can I do this in Fortran?

francescalus
  • 30,576
  • 16
  • 61
  • 96
AlphaBetaGamma96
  • 567
  • 3
  • 6
  • 21
  • Possible duplicate of [How to flush stdout in Fortran 90?](https://stackoverflow.com/questions/37471238/how-to-flush-stdout-in-fortran-90) – Alexander Vogt Feb 08 '18 at 19:18
  • thanks for the referring question, I must've missed that. I've added a "call flush(6)" but how would use carriage return to make the flush command flushes the line and not just bits after what's already there? – AlphaBetaGamma96 Feb 08 '18 at 19:29
  • `char(13)` is the ASCII carriage return. You can add this to the string you want to print. See https://gcc.gnu.org/onlinedocs/gfortran/CHAR.html for details – Alexander Vogt Feb 08 '18 at 20:29

1 Answers1

2

A carriage-return is inserted by default at the end of a write statement, or explicitly using the ADVANCE specifier:

write(unit , '(A)') 'Hello World'
write(unit , '(A)' , ADVANCE = 'YES') 'Hello World'
write(unit , '(A)' , ADVANCE = 'YES') '-'

The result of the preceding three statements is:

Hello World
Hello World
-

Alternatively, the carriage-return may be suppressed by specifying ADVANCE='NO':

write(unit , '(A)' , ADVANCE = 'NO') 'Hello World'
write(unit , '(A)' , ADVANCE = 'NO') 'Hello World'
write(unit , '(A)' , ADVANCE = 'NO') '-'

Which yields:

Hello WorldHello World-

To flush in Fortran90, close the file and reopen. Specific compilers and/or newer versions of the Fortran standard offer FLUSH as an intrinsic, but closing the file will always have the effect of flushing regardless of your Fortran version.

whit
  • 131
  • 6