J3/02-181 Date: April 26, 2002 To: J3 From: Dick Hendrickson Subject: FLUSH for files A frequent topic on Comp.Lang.Fortran is "How do I flush a file in Fortran?" The answer is "you can't." I propose adding a FLUSH= keyword to the OPEN, INQUIRE, and WRITE statements (but not READ where it makes no sense). For OPEN the options would be FLUSH="NO" or FLUSH="YES", with "NO" the default. If "YES" is specified the processor will "flush the buffers" (whatever that means) after every output operation. The set of files that is flushable is processor dependent. The words will need to be modeled after VOLATILE. A processor that is flushing must make a good faith effort to do the actual physical output as soon as it can. It should not be an error condition to request flushing on a file that can't be flushed. For INQUIRE the option would be FLUSH=scalar-default-char-variable with the responses being either "YES" or "NO". The processor will return "NO" if the file was not opened with FLUSH="YES" or if the file isn't in the set of files that can be flushed--regardless of the OPEN specifier. For WRITE add a FLUSH="YES" or "NO" keyword that overrides the OPEN specification. If a file isn't in the set than can be flushed the keyword is ignored. It's a compile time syntax error to specify FLUSH for internal files or on READ statements. I'm not sure what to do for derived type or asynchronous write statements; ignoring FLUSH= or making it illegal both sound reasonable. We'd add enough words to the text description to make it clear that the processor doesn't really have to do anything, but that it should try to do the right thing. One question is whether or not WRITE(ADVANCE="NO" ...) followed by a READ isn't good enough. I've been told that there are different implementations of this sequence and some of them are surprising to some users. Also, I've been told that "advance = 'no'" followed by a read to the same unit is what often triggers the output. This is OK for output to a screen followed by a read from the terminal, but sometimes people want to use advance = 'no' to write a progress bar on the screen, and this doesn't work unless the output is immediately flushed to the screen. Flush lets people say what they mean and not depend on a quirk of implementation. Flush is also useful for debugging of a program that crashes. Leaving the results of WRITE statements in internal buffers isn't much help here. If we want to do this, I think writing the actual rules and text will be relatively simple.