Project

General

Profile

Feature #1487

EOF for OpenIFile

Added by Julian Danner over 3 years ago. Updated over 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Manual/documentation
Target version:
Start date:
15 Sep 2020
Due date:
% Done:

100%

Estimated time:
1.99 h
Spent time:

Description

To read a local file one needs to open an input device via D:=OpenIFile("path_to_file") and read it line-by-line via GetLine(D). However, when the end of the file is reached, there is no indication of that, as GetLine still returns end-of-line characters. Instead of this behavior, I suggest that GetLine throws an error such that there is no ambiguity if the file's end has been reached.

History

#1 Updated by John Abbott over 3 years ago

  • Category set to Manual/documentation
  • Status changed from New to In Progress
  • Target version set to CoCoA-5.4.0
  • % Done changed from 0 to 20

According to the CoCoA-5 manual, GetLine returns a line without the following end-of-line character (ascii code 10 on my computer).
OBSOLETE: However, if one uses GetLine to read beyond the end of a file then a string containing just an end-of-line character is returned, and this is the signal that reading failed.

The source code Interpreter.C:769 has a comment that throwing an exception ended up causing a crash (no idea why; I have also not verified).

WRONG/OBSOLETE: (correct soln is in comment 4 below) So it seems that the appropriate CoCoA-5 test for having reached end of input is:

str := GetLine(D);
if str = "\n" then /*EOF reached*/; endif

Let me know if this works for you.

#2 Updated by John Abbott over 3 years ago

I have improved the manual page for GetLine.

I have not understood why an empty string is returned when EOF has been reached; is that a bug or feature?

PS I have put in a commented out line which would yield "\n" when EOf has been reached after reading an empty line; do we want this?

#3 Updated by John Abbott over 3 years ago

  • Status changed from In Progress to Resolved
  • Assignee set to John Abbott
  • % Done changed from 20 to 80

Is seems that the function IsAtEOF was not in the CoCoA manual; it is now!

This should surely resolve the problem!

#4 Updated by John Abbott over 3 years ago

So now that IsAtEOF is visible, should attempting to read beyond EOF trigger an error?
Probably "yes".

Opinions? Comments?

#5 Updated by Julian Danner over 3 years ago

oh thanks, did not now that this function exists.
I also think that an error is appropriate, since there is no more next line that could be queried.

#6 Updated by John Abbott over 3 years ago

I have improved the man page for GetLine -- it no refers also to the page for IsAtEOF.

#7 Updated by John Abbott over 3 years ago

  • Status changed from Resolved to Closed
  • % Done changed from 80 to 100
  • Estimated time set to 1.99 h

Resolved: use the function IsAtEOF (which existed but was not documented).

Also available in: Atom PDF