Sign in Join
Wiki copytemporary

Assign

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.

Contents

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.

pascal
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

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


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.

Revision 2, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.