Input and Output

These routines can be used on any files:

 CloseCloses an open file

EOF Returns a file's end-of-file status

Open Opens a file for reading or creates

a new file for writing

Reset Opens a file for reading

Rewrite Opens a file for writing

These routines provide random and sequential

access to record oriented files and non-text files:

GetReads a record

Put Writes a record

Read Reads an item

Write Writes an item

Seek Jumps to an item within a file

These routines are used on text-oriented files:

EOLn Returns a text file's end-of-line status

Get Reads a record

Put Writes a record

Read Reads a piece of formatted text

Readln Reads a line of formatted text

Write Formats then writes a piece of text

Writeln Formats then writes a line of text

Files of type TEXT are distinguished from other kinds of files (for example, FILE OF CHAR) by the special significance given to the end-of-line character. The end-of-line character allows a file of type TEXT to be treated as a sequence of lines, rather than a sequence of individual characters. An entire line may be read from the file into a STRING type (note that STRING variables are limited to 255 characters, the maximum size for this data type) using the ReadLn procedure, and an entire line may be written to the file by using the WriteLn procedure. You can test for the end-of-line character by using the EOLn function. When the value of the logical record at the current file position of a file is an end-of-line character, the Read and ReadLn procedures read it as a space character (ASCII 32). The Read and Write procedures can be applied to any typed file. However, the procedures ReadLn and WriteLn depend upon the presence of the end-of-line character, which appears only in files of type TEXT.

None of the text-oriented predefined procedures and functions need an explicit file variable parameter. If no file is named, one of the pre-defined files, input or output, will be assumed by default, depending on whether the procedure or function is input-oriented or output-oriented. Remember that input and output are predeclared as files of type TEXT.

Fichier texte

PROGRAM EX1;

VAR
   f : TEXT;
   fileName, textIn : STRING[255];
   count : INTEGER;

BEGIN

   Write('Enter a file name: ');
   ReadLn(fileName);
   { Create a new, empty file }
   Rewrite(f, fileName);
   { Write to the new, empty file }
   FOR count := 1 TO 10 DO
      WriteLn(f, count);
   { Close the new file }
   Close(f);
   { Open the existing file }
   Open(f, fileName);
   { Send the file to screen }
   WHILE NOT EOF(f) DO
   BEGIN
      ReadLn(f, textIn);
      WriteLn(textIn);
   END;
   { Close the existing file }
   Close(f);

END.

Fichier à accès séquentiel

PROGRAM EX2;

VAR
   f: FILE OF INTEGER;
   i, j: INTEGER;

BEGIN

   writeln('Ecriture');
   rewrite(f, 'list');
   FOR i := 1 TO 10 DO
   BEGIN
      writeln(i);
      f^ := i;
      put(f);
   END;
   close(f);
   writeln('Lecture');
   reset(f, 'list');
   WHILE NOT EOF(f) DO
   BEGIN
      writeln(f^);
      get(f);
   END;
   close(f);

END.

Fichier à accès direct

PROGRAM EX3;

TYPE
   paire = RECORD
      g, d: INTEGER;
   END;

VAR
   p: paire;
   f: FILE OF paire;
   i: INTEGER;

BEGIN

   Open(f, 'test');
   FOR i := 0 TO 9 DO
   BEGIN
      p.g := i;
      p.d := i * i;
      f^ := p;
      Put(f);
   END;
   Close(f);
   Open(f, 'test');
   FOR i := 9 DOWNTO 0 DO
   BEGIN
      Seek(f, i);
      Writeln(f^.g : 3, f^.d : 5);
   END;
   Close(f);

END.

Conseils de programmation

Àpropos de ce document...

This document was generated using the LaTeX2HTML translator Version 96.1 (Feb 5, 1996) Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.

The command line arguments were:
latex2html -split 0 files.tex.

The translation was initiated by Jean-Christophe Soulie on mardi, 4 mai 1999, 17:04:24 GMT+4


Jean-Christophe Soulie
mardi, 4 mai 1999, 17:04:24 GMT+4