Programmiersprache Pascal

Beispiele zur Arbeit mit Dateien

Gnu Pascal

Lesen aus einer vorhandenen Textdatei
Variante 1:

  PROGRAM read_file(OUTPUT);
  VAR f : TEXT;
      s : STRING(80);
  BEGIN
    Reset(f, 'test.txt');             { zu lesen ist Datei test.txt }
    WHILE NOT Eof(f) DO BEGIN
      Readln(f, s);
      Writeln(s);
    END;
  END.
Variante 2:
  PROGRAM read_file(OUTPUT,f);
  VAR f : TEXT;
      s : STRING(80);
  BEGIN
    Reset(f);                         { zu lesen ist Datei f }
    WHILE NOT Eof(f) DO BEGIN
      Readln(f, s);
      Writeln(s);
    END;
  END.
Variante 3:
  PROGRAM read_file(OUTPUT,f);
  VAR f : BINDABLE TEXT;
      b : BindingType;
      s : STRING(80);
  BEGIN
    Unbind(f);
    b := Binding(f);
    b.name := 'test.txt';
    Bind(f, b);
    Reset(f);
    WHILE NOT Eof(f) DO BEGIN
      Readln(f, s);
      Writeln(s);
    END;
  END.

Erweitern einer Textdatei
Variante 1:

  PROGRAM append_file(f);

  VAR f: TEXT;

  BEGIN
    Extend(f, 'text.txt');              { Erweiterte Syntax }
    Writeln(f, 'angefuegter Text');
    Close(f);
  END.
Variante 2:
  PROGRAM append_file(f);

  VAR f: TEXT;

  BEGIN
    Extend(f);                          { Dateiname wird interaktiv erfragt }
    Writeln(f, 'angefuegter Text');
    Close(f);
  END.
Beim Programmstart wird der Name zu eröffnenden Datei abgefragt.



P. Böhme, 05.09.1996