Get: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | The [[Procedure|procedure]] **`get`** retrieves a datum from a [[typed files|file of …]] and advances the reading cursor. Although `get` is a standardized [[Standard Pascal|Pascal]] routine, in the [[FPC]] `get` is only available in an ISO-compliant mode, such as [[Mode iso|{$mode ISO}]]. | |
| 2 | 2 | ||
| 3 | 3 | ## behavior | |
| 4 | 4 | ||
| 5 | 5 | ### requirements | |
| 6 | 6 | ||
| 7 | 7 | Before `get` is executed, two requirements must be met: | |
| 8 | 8 | ||
| 9 | 9 | 1. The file must be in *inspection* mode. | |
| 10 | 10 | 2. The file’s “cursor” may not be past [[EOLN and EOF|EOF]]. | |
| 11 | 11 | ||
| 12 | 12 | A [[runtime error|run-time error]] occurs if either condition is not satisfied. | |
| 13 | 13 | ||
| 14 | 14 | ### effect | |
| 15 | 15 | ||
| 16 | 16 | `Get` | |
| 17 | 17 | ||
| 18 | 18 | 1. advances the cursor by one record | |
| 19 | 19 | 2. makes the new current record accessible via the file’s buffer variable. | |
| 20 | 20 | ||
| 21 | 21 | To put this into relation: | |
| 22 | 22 | ||
| 23 | 23 | - `read(f, x)` is equivalent to `x := f^; get(f)`. | |
| 24 | 24 | - `readLn(f)` is equivalent to `while not EOLn(f) do get(f); get(f)`. The latter `get(f)` actually consumes the newline marker. | |
| 25 | 25 | - `reset(f)` is semantically equivalent to `seekRead(f, pred(firstRecord)); get(f)` where `firstRecord` is the index of the first record. | |
| 26 | 26 | ||
| 27 | 27 | ## application | |
| 28 | 28 | ||
| 29 | 29 | `Get` is especially used to | |
| 30 | 30 | ||
| 31 | 31 | - read the file buffer variable multiple times at various locations (e. g. subroutines), | |
| 32 | 32 | - eliminate the need for an *extra* buffer variable. | |
| 33 | 33 | ||
| 34 | 34 | begin | |
| 35 | 35 | ||
| 36 | 36 | ` while not EOF(input) do`\ | |
| 37 | 37 | ` begin`\ | |
| 38 | 38 | ` output^ := input^;`\ | |
| 39 | 39 | ` get(input);`\ | |
| 40 | 40 | ` put(output);`\ | |
| 41 | 41 | ` end;` | |
| 42 | 42 | ||
| 43 | 43 | end. | |
| 44 | 44 | ||
| 45 | - | ||
| 46 | - | ||
| 47 | 45 | - bypass [[Read|read/readLn]]’s interpretation/conversion capabilities and make nasty [[Typecast|type casts]]. | |
| 48 | 46 | ||
| 49 | 47 | ## see also | |
| 50 | 48 | ||
| 51 | 49 | - [[Put|put]] | |
| 52 | 50 | ||
| 53 | 51 | --- | |
| 54 | 52 | *From the Free Pascal wiki, page "Get", as it stood on 2021-07-20 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).* | |
| 55 | 53 | Category: File Handling | |
| 56 | 54 | Category: Pascal |
