Chapter 6: Advanced Pascal Programming
This chapter covers some more advanced parts of Pascal: file handling, dynamic memory allocation, and pointers. You’ll use them when your programs need to manage more complex data and interactions.
File handling
Pascal has several built-in procedures and functions for file handling. A file stores data permanently, so you can read it back whenever you need it.
Types of files
Pascal supports different types of files, including:
- Text files, which store readable characters.
- Binary files, which store data in binary format.
Text files
Declaring text files
var
fileName: text;
Writing to a text file
program WriteTextFile;
var
fileName: text;
i: integer;
begin
assign(fileName, 'output.txt');
rewrite(fileName);
for i := 1 to 10 do
writeln(fileName, 'Line number ', i);
close(fileName);
end.
Reading from a text file
program ReadTextFile;
var
fileName: text;
line: string;
begin
assign(fileName, 'output.txt');
reset(fileName);
while not eof(fileName) do
begin
readln(fileName, line);
writeln(line);
end;
close(fileName);
end.
Binary files
Declaring binary files
var
binFile: file of dataType;
Writing to a binary file
program WriteBinaryFile;
type
Student = record
name: string[20];
age: integer;
end;
var
binFile: file of Student;
s: Student;
begin
assign(binFile, 'students.dat');
rewrite(binFile);
s.name := 'Alice';
s.age := 22;
write(binFile, s);
s.name := 'Bob';
s.age := 24;
write(binFile, s);
close(binFile);
end.
Reading from a binary file
program ReadBinaryFile;
type
Student = record
name: string[20];
age: integer;
end;
var
binFile: file of Student;
s: Student;
begin
assign(binFile, 'students.dat');
reset(binFile);
while not eof(binFile) do
begin
read(binFile, s);
writeln('Name: ', s.name, ', Age: ', s.age);
end;
close(binFile);
end.
Dynamic memory allocation
Dynamic memory allocation lets you allocate memory at runtime. That helps when you can’t know at compile time exactly how much memory the program will need.
Using the new and dispose procedures
program DynamicMemory;
var
p: ^integer;
begin
new(p); { Allocate memory }
p^ := 10;
writeln('Value: ', p^);
dispose(p); { Free memory }
end.
Here p is a pointer to an integer. The new procedure allocates memory for an integer, and dispose frees that memory again.
Dynamic arrays
With a dynamic array, you allocate and manage the array at runtime.
program DynamicArray;
var
arr: array of integer;
i, n: integer;
begin
writeln('Enter the size of the array: ');
readln(n);
setlength(arr, n);
for i := 0 to n - 1 do
arr[i] := i * 2;
for i := 0 to n - 1 do
writeln('arr[', i, '] = ', arr[i]);
end.
Pointers
A pointer is a variable that stores the memory address of another variable. Pointers are used for dynamic memory allocation and for accessing variables indirectly.
Declaring pointers
var
p: ^integer;
Using pointers
program PointerExample;
var
p, q: ^integer;
x: integer;
begin
new(p);
p^ := 10;
writeln('Value at p: ', p^); { Output: 10 }
x := 20;
q := @x;
writeln('Value at q: ', q^); { Output: 20 }
dispose(p);
end.
p and q are both pointers to integers. The new procedure allocates memory for an integer, and the @ operator gets the address of a variable.
Pointer arithmetic
Standard Pascal has no pointer arithmetic. Free Pascal adds it: Inc(p) on a typed pointer moves it forward by the size of the type it points to, as p++ does in C.
program PointerIncrement;
var
numbers: array[1..3] of integer = (10, 20, 30);
p: ^integer;
i: integer;
begin
p := @numbers[1];
for i := 1 to 3 do
begin
writeln(p^);
Inc(p); { Move to the next integer, not the next byte }
end;
end.
Most Pascal code doesn’t need this. It’s usually clearer to keep a pointer to a whole array and index into it, as this example does:
program PointerArithmetic;
type
IntArray = array[1..10] of integer;
PIntArray = ^IntArray;
var
arr: PIntArray;
i: integer;
begin
new(arr);
for i := 1 to 10 do
arr^[i] := i * 3;
for i := 1 to 10 do
writeln('arr[', i, '] = ', arr^[i]);
dispose(arr);
end.
arr points to an array allocated with new, and arr^[i] reaches each element through an index, so no pointer arithmetic is needed.