[Overview][Constants][Types][Classes][Procedures and functions][Variables] Reference for unit 'System' (#rtl)

Rewrite

Open file for writing

Declaration

Source position: line 0

procedure Rewrite(

  var f: ;

  l: LongInt

);

procedure Rewrite(

  var f:

);

procedure Rewrite(

  var f: TypedFile

);

procedure Rewrite(

  var t: Text

);

Description

Rewriteopens a file Ffor writing. Fcan be any file type. If Fis an untyped or typed file, then it is opened for reading and writing. If Fis an untyped file, the record size can be specified in the optional parameter L. Default a value of 128 is used. if Rewritefinds a file with the same name as F, this file is truncated to length 0. If it doesn't find such a file, a new file is created. Contrary to Turbo Pascal, Free Pascal opens the file with mode fmoutput. If it should be opened in fminoutmode, an extra call to Resetis needed. File sharing is not taken into account when calling Rewrite.

Errors

Depending on the state of the \var{\{\$I\}} switch, a runtime error can be generated if there is an error. In the \var{\{\$I-\}} state, use IOResultto check for errors.

See also

Reset

  

Open file for reading

Assign

  

Assign a name to a file

Close

  

Close a file

Flush

  

Write file buffers to disk

Append

  

Open a file in append mode

Example

Program Example52;

{ Program to demonstrate the Rewrite function. }

Var F : File;
    I : longint;

begin
  Assign (F,'Test.tmp');
  { Create the file. Recordsize is 4 }
  Rewrite (F,Sizeof(I));
  For I:=1 to 10 do
    BlockWrite (F,I,1);
  close (f);
  { F contains now a binary representation of
    10 longints going from 1 to 10 }
end.