RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.Flush Function

Empties the buffer of a text file opened for output.

Pascal
function Flush(var t: Text): Integer;
C++
int Flush(Text t);

F is a text file variable. 

When a text file is opened for output using Rewrite or Append, Flush empties the file's buffer. This guarantees that all characters written to the file at that time have actually been written to the external file. Flush has no effect on files opened for input. 

Flush returns 0 if the operation succeeded. Otherwise, it returns the error code. When compiled using the {$I-} flag, the IOResult method returns this value.

Note: {$I+} handles runtime errors using exceptions. When using {$I-}, use IOResult to check for I/O errors.
 

Delphi Examples: 

 

{
Click the button and select a file to append a string to the bottom of the file.
}

procedure TForm1.Button1Click(Sender: TObject);
var
  f: TextFile;
begin
  if OpenDialog1.Execute then
  begin                    { open a text file }
    AssignFile(f, OpenDialog1.FileName);
    Append(f);
    Writeln(f, 'I am appending some stuff to the end of the file.'); 
    { insert code here that would require a Flush before closing the file }
    Flush(f);  { ensures that the text was actually written to file }
    CloseFile(f);
    MessageDlg(OpenDialog1.FileName + ' has been altered, please validate.', mtInformation, [mbOK], 0)
  end;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!