
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.
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.
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.
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:
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].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.
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
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?TEXT[8] back, and take out line 50. What is the first line shown?'A:NOSUCH .TXT'. What does SHOW print?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.
| Symptom | Cause |
|---|---|
| Odd characters at the starts of lines read from a file | The precaution: a bigger buffer, and a spare first line. |
| The last lines of a file are never read | A lost character was read as the end of the file. The same cure. |
| A missing file gives no error, just nothing | RESET of a missing file is not an error. Test EOF. |
Errors at a TEXT inside a procedure | Files may only be declared in the main program. |
| A file of records is refused | Only text files. Write the fields as text. |
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.
S13, The Heap - NEW, and how memory is given back when there is no Dispose.