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## usage
44
55In 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.
66
77```delphi
88program assignDemo(input, output, stdErr);
99var
1010 FD: text;
1111begin
1212 assign(FD, '/tmp/assignDemo.txt');
1313 assign(FD, '/root/.bashrc');
1414end.
1515```
1616
1717Note, 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.
1818
1919## application
2020
2121- `Assign` is used before operating on any file variable.
2222
2323var
2424
2525`   executable: file of Byte;`
2626
2727begin
2828
2929`   assign(executable, paramStr(0));`\
3030``    fileMode := 0; { 0 = read-only; only relevant for `file of …` variables } ``\
3131`   reset(executable);`\
3232`   writeLn(paramStr(0), ' has a size of ', fileSize(executable), ' Bytes.');`
3333
3434end.
3535
36-</syntaxhighlight>
37-
3836- `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:
3937
4038const
4139
4240`   blackhole = {$ifDef Linux}   '/dev/null' + {$endIf}`\
4341`               {$ifDef Windows} 'nul'       + {$endIf}`\
4442`               '';`
4543
4644begin
4745
4846`   assign(output, blackhole);`\
4947`   rewrite(output);`\
5048`   writeLn('Hello world!');`
5149
5250end.
53-
54-</syntaxhighlight>
5551
5652## caveats
5753
5854- 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).
5955- 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.
6056
6157## notes
6258
6359- 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.
6460- In [[Extended Pascal]] a combination of `bindingType` and `bind` are usually used to achieve linking a file variable with an external entity.
6561
6662---
6763*From the Free Pascal wiki, page "Assign", as it stood on 2022-09-03 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. 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).*
6864Category: Code
6965Category: Delphi
7066Category: File Handling

Back to history