Sign in Join
Wiki copytemporary

Assign: what changed

Revision 1 to 2. 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
33## Contents
44
55 * 1 usage
66 * 2 application
77 * 3 caveats
88 * 4 notes
99
1010## usage
1111
1212In 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.
1313```
1414program assignDemo(input, output, stdErr);
1515 var
1616 FD: text;
1717 begin
1818 assign(FD, '/tmp/assignDemo.txt');
1919 assign(FD, '/root/.bashrc');
2020 end.
2121```
2222
2323Note, 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.
2424
2525## application
2626
2727 * `Assign` is used before operating on any file variable.
2828```
2929program assignApplication(input, output, stdErr);
3030 var
3131 executable: file of Byte;
3232 begin
3333 assign(executable, paramStr(0));
3434 fileMode := 0; { 0 = read-only; only relevant for `file of …` variables }
3535 reset(executable);
3636 writeLn(paramStr(0), ' has a size of ', fileSize(executable), ' Bytes.');
3737 end.
3838```
3939
4040 * `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:
4141```
4242program redirectDemo(input, output, stdErr);
4343 const
4444 blackhole = {$ifDef Linux} '/dev/null' + {$endIf}
4545 {$ifDef Windows} 'nul' + {$endIf}
4646 '';
4747 begin
4848 assign(output, blackhole);
4949 rewrite(output);
5050 writeLn('Hello world!');
5151 end.
5252```
5353
5454## caveats
5555
5656 * 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>).
5757 * 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.
5858
5959## notes
6060
6161 * 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.
6262 * In [[Extended Pascal]] a combination of `bindingType` and `bind` are usually used to achieve linking a file variable with an external entity.
6363
6464---
65-*From the Free Pascal wiki, page "AssignFile", as the Internet Archive kept it on 2025-05-17. 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).*
65+*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