Assign

This is an old revision of Assign, edited by Kai Burghardt at . The current revision is 146186.

fix program

The Borland Pascal procedure assign 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.

usage

In 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.

program assignDemo(input, output, stdErr);
var
    FD: text;
begin
    assign(FD, '/tmp/assignDemo.txt');
    assign(FD, '/root/.bashrc');
end.

Note, 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(0)) do not pose a problem to assign. It merely operates on internal management data.

application

program assignApplication(input, output, stdErr);
var
    executable: file of Byte;
begin
    assign(executable, paramStr(0));
    fileMode := 0; { 0 = read-only; only relevant for `file of …` variables }
    reset(executable);
    writeLn(paramStr(0), ' has a size of ', fileSize(executable), ' Bytes.');
end.
program redirectDemo(input, output, stdErr);
const
    blackhole = {$ifDef Linux}   '/dev/null' + {$endIf}
                {$ifDef Windows} 'nul'       + {$endIf}
                '';
begin
    assign(output, blackhole);
    rewrite(output);
    writeLn('Hello world!');
end.

caveats

notes