Flushing+file+buffers

//by Richard Russell, June 2010//

When you write to a sequential data file using [|PRINT#] or [|BPUT#] the data is **buffered** by BBC BASIC before being output to Windows (typically for storage on disk). This is done for reasons of performance, because transferring data in blocks is more efficient than transferring it one byte at a time. A similar process occurs when you are reading from a file.

These buffering operations normally happen transparently, and you don't need to be aware of them. However there are a couple of circumstances in which the existence of the buffers can matter to your BBC BASIC program:
 * 1) If you are mixing BASIC file-access statements (e.g. **PRINT#**, **BPUT#**) with Windows API calls (e.g. **SYS "WriteFile"**) which access the same file, it is important that the buffer be flushed in between using one variety of access and the other.
 * 2) If the file you are creating is shared between two processes (e.g. one program writing to the file and another program reading from the file) it is necessary to flush the buffers to ensure that all the data written by one program can be read by the other program.

In both cases flushing BBC BASIC's file buffer is simply a case of issuing the following statement: code format="bb4w" PTR#chan% = PTR#chan% code where **chan%** is the channel number returned by [|OPENIN], [|OPENOUT] or [|OPENUP].