Sign in Join
Wiki copytemporary

Assign: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11The [[Borland Pascal]] [[Procedure|`procedure`]] [`assign`](<https://www.freepascal.org/docs-html/rtl/system/assign.html>) associates a file variable with an external entity (e. g. a device file or a file on disk). `Assign` only operates on the program’s internal management data and has otherwise no immediate observable effect.
22
3-## Contents
4-
5- * 1 usage
6- * 2 application
7- * 3 caveats
8- * 4 notes
9-
103## usage
114
125In non-ISO modes the [[FPC]] needs files to be explicitly associated with external entities. This can be achieved with `assign`. The first parameter is a file variable, the second parameter specifies a path.
136```
147program assignDemo(input, output, stdErr);
158 var
169 FD: text;
1710 begin
1811 assign(FD, '/tmp/assignDemo.txt');
1912 assign(FD, '/root/.bashrc');
2013 end.
2114```
2215
2316Note, you can run this program without any troubles as any user on any platform. Nonexistent path components (e. g. directories), missing privileges to eventually access the file or any path component, or unacceptable characters (usually [[Chr|`chr(0)`]]) do not pose a problem to `assign`. It merely operates on internal management data.
2417
2518## application
2619
2720 * `Assign` is used before operating on any file variable.
2821```
2922program assignApplication(input, output, stdErr);
3023 var
3124 executable: file of Byte;
3225 begin
3326 assign(executable, paramStr(0));
3427 fileMode := 0; { 0 = read-only; only relevant for `file of …` variables }
3528 reset(executable);
3629 writeLn(paramStr(0), ' has a size of ', fileSize(executable), ' Bytes.');
3730 end.
3831```
3932
4033 * `Assign` can be used to _redirect_ standard files [`input`](<https://www.freepascal.org/docs-html/rtl/system/input.html>)/[`output`](<https://www.freepascal.org/docs-html/rtl/system/output.html>). Our [[Hello, World]] program needs only a few changes as highlighted:
4134```
4235program redirectDemo(input, output, stdErr);
4336 const
4437 blackhole = {$ifDef Linux} '/dev/null' + {$endIf}
4538 {$ifDef Windows} 'nul' + {$endIf}
4639 '';
4740 begin
4841 assign(output, blackhole);
4942 rewrite(output);
5043 writeLn('Hello world!');
5144 end.
5245```
5346
5447## caveats
5548
5649 * Regardless of the utilized [[Character and string types|string data type]], `assign` can only process paths of up to 255 [[Byte|Bytes]]. This limitation comes from the internally used [`fileRec`](<https://www.freepascal.org/docs-html/rtl/system/filerec.html>)/[`textRec`](<https://www.freepascal.org/docs-html/rtl/system/textrec.html>). Relative paths in conjunction with working directories cannot overcome this. Cf. [FPC issue 35185](<https://gitlab.com/freepascal.org/fpc/source/-/issues/35185>).
5750 * Since `assign` only operates on internal management data, specifying illegal paths is accepted. It is even possible to supply an empty string as a path. The specific behavior on a file variable bearing an empty file name depends on the used I/O routine.
5851
5952## notes
6053
6154 * If `{$modeSwitch objPas+}` (automatically set via [[Mode ObjFPC|`{$mode objFPC}`]] or [[Mode Delphi|`{$mode Delphi}`]]), the `procedure` `assignFile` is available, too. It performs the very same task.
6255 * In [[Extended Pascal]] a combination of `bindingType` and `bind` are usually used to achieve linking a file variable with an external entity.
6356
6457---
6558*From the Free Pascal wiki, page "AssignFile", as the Internet Archive kept it on 2025-05-17, last changed on 2022-09-03. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history