Get

From Free Pascal wiki
Jump to navigationJump to search

English (en)

The procedure get retrieves a datum from a file of  and advances the reading cursor. Although get is a standardized Pascal routine, in the FPC get is only available in an ISO-compliant mode, such as {$mode ISO}.

behavior

requirements

Before get is executed, two requirements must be met:

  1. The file must be in inspection mode.
  2. The file’s “cursor” may not be past EOF.

A run-time error occurs if either condition is not satisfied.

effect

Get

  1. advances the cursor by one record
  2. makes the new current record accessible via the file’s buffer variable.

To put this into relation:

  • read(f, x) is equivalent to x := f^; get(f).
  • readLn(f) is equivalent to while not EOLn(f) do get(f); get(f). The latter get(f) actually consumes the newline marker.
  • reset(f) is semantically equivalent to seekRead(f, pred(firstRecord)); get(f) where firstRecord is the index of the first record.

application

Get is especially used to

  • read the file buffer variable multiple times at various locations (e. g. subroutines),
  • eliminate the need for an extra buffer variable.
    program echo(input, output);
    begin
    	while not EOF(input) do
    	begin
    		output^ := input^;
    		get(input);
    		put(output);
    	end;
    end.
    
  • bypass read/readLn’s interpretation/conversion capabilities and make nasty type casts.

see also