(* Collection of code snippets by Arne Vajhøj *)
(* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) *)
program rel(input,output);

type
   string = varying [255] of char;

var
   f : file of string;

procedure test_write_one(ix : integer; val : string);

begin
   locate(f, ix);
   f^ := val;
   put(f);
end;

procedure test_write;

begin
   open(f, 'pas.rel', new,
        organization := relative, record_length := 504, record_type := variable,
        access_method := direct);
   rewrite(f);
   test_write_one(3, 'CCC');
   test_write_one(2, 'BB');
   test_write_one(1, 'A');
   close(f);
end;

procedure test_read_sequential;

var
   val : string;

begin
   open(f, 'pas.rel', old,
        organization := relative, record_length := 504, record_type := variable,
        access_method := sequential);
   reset(f);
   while not(eof(f)) do begin
      val := f^;
      writeln('|' + val + '|', length(val));
      get(f);
   end;
   close(f);
end;

procedure test_read_random_one(ix : integer);

var
   val : string;

begin
   find(f, ix);
   val := f^;
   writeln('|' + val + '|', length(val));   
end;

procedure test_read_random;

begin
   open(f, 'pas.rel', old,
        organization := relative, record_length := 504, record_type := variable,
        access_method := direct);
   test_read_random_one(3);
   test_read_random_one(2);
   test_read_random_one(1);
   close(f);
end;

begin
   writeln('Pascal');
   test_write;
   test_read_sequential;
   test_read_random;
end.
