HiSoft Pascal Primer cover: a square, a triangle and a circle drawn and filled by a HiSoft Pascal program on the Tatung Einstein
← Back to Courses
module
12

Files

Introduction

The Turbo course's S21 kept records on the disc with Assign, Rewrite, Close and file of. HiSoft Pascal's files are simpler and rougher: text files only, named as they are opened, and never closed - and reading them back needs one precaution, without which characters go missing.

Writing

A text file is a TEXT variable (which may only be declared in the main program, not inside a procedure). There is no Assign: the disc name goes straight into REWRITE:

REWRITE(F,'A:NOTES   .TXT');

The name must be exactly 14 characters: the drive and a colon, the name padded with spaces to eight, a full stop, and three for the type. Two spaces in place of A: mean the logged drive.

Then WRITE(F,...) and WRITELN(F,...) write to it, just as they write to the screen - numbers, text, anything WRITE can print. There is no Close. When the program ends, the file is finished and on the disc.

Only text: there is no file of a record. To keep a record, write its fields as a line of text, and read them back the same way.

Reading

RESET(F,'A:NOTES .TXT') opens the file to read, and READ(F,...), READLN(F), EOLN(F) and EOF(F) work on it as on the keyboard.

A RESET of a file that is not there is not an error - where Turbo stopped with I/O error 01. EOF(F) is simply true from the start. So test EOF straight after RESET if the file might be missing.

The Precaution

When HiSoft Pascal reads a file from the disc, the first three characters of each piece it reads come back wrong. It reads a file into memory a piece at a time - 128 characters at a time, unless you say otherwise - and the first three characters of every piece arrive as something else. It happens every time, and nothing reports it. In a file of thirty short lines, a dozen characters went wrong - and one of them was read as the end of the file, so the last line was never reached at all.

Two things together get round it:

  1. Give the file a buffer bigger than the file. A number in brackets after the type says how many 128-character pieces to read at once: F:TEXT[8] takes up to 1,024 characters in one go. If the whole file fits, it is read in one piece, and only its first three characters are lost. The most is [255].
  2. Begin every file you write with a spare line, and skip it with READLN(F) straight after the RESET. Its first three characters are the ones that are lost.

With both, the same thirty lines read back without a character wrong.

The Code

Two programs: one writes a file, the other reads it.

10 PROGRAM NOTES;
20 VAR F:TEXT; I:INTEGER;
30 BEGIN
40 REWRITE(F,'A:NOTES   .TXT');
50 WRITELN(F,'(SPARE LINE)');
60 FOR I:=1 TO 30 DO
70 WRITELN(F,'NOTE NUMBER ',I);
80 WRITELN('WRITTEN')
90 END.
10 PROGRAM SHOW;
20 VAR F:TEXT[8]; C:CHAR; N:INTEGER;
30 BEGIN
40 RESET(F,'A:NOTES   .TXT');
50 READLN(F);
60 N:=0;
70 WHILE NOT EOF(F) DO
80 BEGIN
90 WHILE NOT EOLN(F) DO
100 BEGIN READ(F,C); WRITE(C) END;
110 READLN(F); WRITELN;
120 N:=N+1
130 END;
140 WRITELN(N,'LINES')
150 END.

Starting from

NOTES.ASC and SHOW.ASC saved from XBAS; HPEIN NOTES, NOTES, then HPEIN SHOW, SHOW.

What you should see

NOTES prints WRITTEN, and DIR shows NOTES.TXT. SHOW lists

NOTE NUMBER 1
NOTE NUMBER 2
...
NOTE NUMBER 30
30 LINES

Change One Thing

  • In SHOW, change line 20's TEXT[8] to TEXT. Look closely at the lines as they go by. How many lines does it say it read?
  • Put TEXT[8] back, and take out line 50. What is the first line shown?
  • Change line 40's file name to 'A:NOSUCH .TXT'. What does SHOW print?

Exercises

12.1 Write a program that asks for five numbers and writes them to a file, one to a line; and a second that reads them back and prints their total.

12.2 Make SHOW say NO NOTES when the file is not there.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
Odd characters at the starts of lines read from a fileThe precaution: a bigger buffer, and a spare first line.
The last lines of a file are never readA lost character was read as the end of the file. The same cure.
A missing file gives no error, just nothingRESET of a missing file is not an error. Test EOF.
Errors at a TEXT inside a procedureFiles may only be declared in the main program.
A file of records is refusedOnly text files. Write the fields as text.

Summary

Files are text only. REWRITE(F,'A:NAME .EXT') and RESET(F,...) take the 14-character name; there is no Assign and no Close, and a missing file is not an error. When reading, the first three characters of each piece read come back wrong: give the file a buffer bigger than itself, TEXT[n], and start it with a spare line that you skip.

Next

S13, The Heap - NEW, and how memory is given back when there is no Dispose.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.