Recent

Author Topic: [SOLVED] ioresult yields a value of 0 in FileRead!!  (Read 994 times)

tfurnivall

  • Jr. Member
  • **
  • Posts: 88
[SOLVED] ioresult yields a value of 0 in FileRead!!
« on: August 26, 2025, 08:54:19 pm »
I am getting a value returned from ioresult, of 0. Which is an (apparently) undocumented value.  Here is the relevant code:
Code: Pascal  [Select][+][-]
  1. procedure GetNextSourceLine();
  2.  
  3. { *** GETNEXTSOURCELINE
  4.   *** Identify the current SourceFile in the SourceFileTable
  5.   *** This is given by the global variable CurrentSource. The entry
  6.   *** in SourceFileTable will include not only the handle for the file,
  7.   *** but also the important information about the file that will make
  8.   *** it possible to know how to read the next line. See tSFTEntry=record }
  9.  
  10. var
  11.   SourceFile    : THandle;
  12.   inputline     : string[MAXSOURCELINELENGTH];  {=120}
  13.   outputline    : string;
  14.   ReadLength    : integer;
  15.   ReadError     : integer;
  16.   msg           : string;
  17. begin
  18.  
  19.   with SourceFileTable[CurrentSource] do
  20.        begin
  21.          SourceFile     := SFTFile;
  22.          ReadError     := 0;
  23.  
  24.         {$i-}
  25.          if CurrentSource = 1 then
  26.             begin
  27.               ReadLength := FileRead
  28.                            (SourceFile, inputline, MAXSOURCELINELENGTH);
  29.             end
  30.          else
  31.             begin
  32.               ReadLength := FileRead
  33.                            (SourceFile, inputline, MAXSOURCELINELENGTH);
  34.             end;
  35.          ReadError       :=ioresult();
  36.         {$i+}
  37.  
  38.          if ReadLength = -1 then
  39.             begin
  40.               msg  := 'Error in FileRead: ' + format('%-10d',[ReadError]);
  41.               write(msg);
  42.               readln()
  43.             end
  44.          else
  45.             begin
  46.               SFTLastLineNum        := SFTLastLineNum + 1;
  47.               SourceLineNum         := SFTLastLineNum;
  48.               SFTLastLinePos        := 0;
  49.               SFTRecNum             := SFTRecNum + 1;
  50.               SFTLastSourceLine     := inputline;
  51.             end;
  52.        end;
  53.  
  54.  

(The distinction between CurrentSource = 1 and CurrentSource = any other value is a 'future consideration').

When I step through the code, I take the first path through the if statement, and get the (un) expected value of -1 in the ReadLength variable. Then, when I try to capture the error code (from ioresult) the returned value is 0.

So now I have a situation where FileRead says "Oops, there's an error here (but I'm not going to tell you what)", and ioresult says "Nothing wrong here - why are you troubling me?"

Notes:

I do not have an explicit uses for the System unit (when I add it I get an error message "Duplicate Identifier" even though it's nowhere to be found)

The examples for FileRead etc all indicate that if an error is detected -1 is returned. The only example of actually testing for this that I found included the fun code:
Code: Pascal  [Select][+][-]
  1. F:= Fi leCreate ( ' t e s t . dat ' ) ;
  2. I f F = -1 then
  3. Halt ( 1 ) ;
which seems more than slightly extreme!

Is it, perhaps, the case that when using the alternate routines (FileOpen, FileClose, etc) the only help you get is a terse Error, and that's it? Or is it the case that ioresult() does not apply to these routines (in which case, what does?) or something else?

Is there a way to find out what error condition resulted in a return code of -1? Or do we simply keep trying /beating our head against a brick wall / or something else?

(Incidentally, my confidence in documentation which describes all of these routines as functions and then conveniently omits to test any return value, is vanishingly close to zero).

Frustrated as usual, sorry for the polemics, any and all suggestions appreciated!

Tony

Turned out I had dropped a character in a filename which I had carefully stored (with the dropped character) in a comment in the source, so that I wouldn't make mistakes typing it in! Too smart for my own good. :-[

« Last Edit: August 31, 2025, 11:48:45 pm by tfurnivall »

Josh

  • Hero Member
  • *****
  • Posts: 1455
Re: ioresult yields a value of 0 in FileRead!!
« Reply #1 on: August 26, 2025, 09:57:31 pm »
Hi
Had a quick look.

Your using a s string as a buffer;note the byte size is 121 bytes..

your using fileread,a binary read, that will read upto 121 bytes, which could be multiple lines.
fileread does it own io checking and returns -1 if error;ie trying to read past eof.
if memory server i think theres a  function getlasterror or somthing like that which may help;

ioresult of 0 indicates no error, hense examples mostly read
err:=ioresult;
if  err<>0 then   errror do something;
note ioresult is read once as it resets when called, i think it reads InoutRes; which is not reset  so this may be available.

Sorry cant help much, just observations
« Last Edit: August 26, 2025, 09:59:44 pm by Josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Nitorami

  • Hero Member
  • *****
  • Posts: 605
Re: ioresult yields a value of 0 in FileRead!!
« Reply #2 on: August 26, 2025, 10:24:56 pm »
I always used read, blockread, write instead, which set IOResult properly. I guess fileread, filewrite are just wrappers around these, hiding the error code. No idea who would need that.

ASerge

  • Hero Member
  • *****
  • Posts: 2492
Re: ioresult yields a value of 0 in FileRead!!
« Reply #3 on: August 26, 2025, 10:58:36 pm »
I am getting a value returned from ioresult, of 0. Which is an (apparently) undocumented value. 
The IOResult function is used only with classic file operations (Read, Write, Reset, Rewrite, etc.). The FileRead function does not save the result to the InOutRes variable.

 

TinyPortal © 2005-2018