Hi again,
While I'm on the topic of exploring the file functions, does
anyone want to explain the function of the various Seek* functions in
extended Pascal? The descriptions in the standard contain a lot of
intelligible text and little in the way of examples. I assumed that
Seekread(file, n) would simply take me to the nth record in 'file,' --
which it does, but then it actually seems to stall the file pointer at
n until you read two items.
Take for example:
program readchar(input, output, inputfile);
var
inputfile : file of char;
letter : char;
i : integer;
begin
reset(inputfile, 'data');
seekread(inputfile, 3);
writeln(inputfile^);
for i := 1 to 5 do
begin
read(inputfile,letter);
writeln(letter);
end;
close(inputfile);
end.
Output:
coltrane:~/Pascal> cat data
ABCDEFGHIJKLMNOPQRSTUVWXYZ
coltrane:~/Pascal> gpc readchar.pas -o readchar
coltrane:~/Pascal> readchar
D
D
D
E
F
G
coltrane:~/Pascal>
I would have expected D D E F G H for output? Why does the file
pointer stay at 3 for two consecutive reads? Is this a bug or am I
misusing seekread()?
Also isn't the typechecking of seekread a little harsh to not allow:
program indexseek(input, output, inputfile);
var
inputfile : file [0..25] of char;
begin
reset(inputfile, 'data');
seekread(inputfile, 3);
close(inputfile);
end.
when it does allow:
program indexseek(input, output, inputfile);
var
inputfile : file [0..25] of char;
index : integer = 3;
begin
reset(inputfile, 'data');
seekread(inputfile, index);
close(inputfile);
end.
Various Miscellany:
When I was playing around with all of this I wrote an ugly bit of code
that could be reduced to:
program test(output);
var i : 3..3;
begin
i := i + 1;
writeln(i);
end.
Why isn't there a warning about i being undefined? This code outputs
1, btw :)
Lastly according to iso7185:6.4.2.4, "The first constant of a
subrange-type shall specify the smallest value, and this shall be less
than or equal to the largest value..." yet gpc happily compiles code
like:
program test(output); var i: 4..1;begin writeln(i) end.
thanks for any explanations about the seekread() stuff,
-Kevin
--
Kevin A. Foss --- kfoss(a)mint.net
--