Next: , Previous: ReturnAddress, Up: Reference


(Under construction.)


     procedure Rewrite (var F: any_file; [FileName: String;]
                                         [BlockSize: Cardinal]);


Rewrite opens a file for writing. If the file does not exist, it is created. The file pointer is positioned at the beginning of the file.

Like Reset, Append and Extend do, Rewrite accepts an optional second and third parameter.

The second parameter can specify the name of the file in the filesystem. If it is omitted, the following alternative ways can be used to specify the name. There are so many different ways in order to be compatible to the idiosyncrasies of as many other Pascal compilers as possible. (If you know about yet other ways, let us know ...)

The following ways are only available if the file is external, i.e. a global variable which is mentioned in the program header. Otherwise, the file will be internal, i.e. get no name in the file system (it may get a name temporarily, but will then be erased automatically again). This is useful to store some data and read them back within a program without the need for permanent storage.

The last optional parameter determines the block size of the file. It is valid only for untyped files. Often 1 is a reasonable value here. However, the existence of this parameter is a BP compatibility feature, and in BP it defaults to 128 because of historic misdesign. Therefore, GPC requires this parameter to be present. In --borland-pascal mode, it makes it optional (like BP does), but warns about the strange default if omitted.

Conforming to

Rewrite is defined in ISO 7185 Pascal. The BlockSize parameter is a Borland Pascal extension. The FileName parameter is a GNU Pascal extension.


     program RewriteDemo;
       Sample: Text;
       Assign (Sample, 'sample.txt');
       Rewrite (Sample);
       WriteLn (Sample, 'Hello, World!');
       Close (Sample)

See also

Assign, Reset, Append, Extend, Update.