[SOLVED] ioresult yields a value of 0 in FileRead!! lazarus / 21-General ---------------------------------------------------------------------- tfurnivall · 2025-08-26 18:54 I am getting a value returned from ioresult, of 0. Which is an (apparently) undocumented value.  Here is the relevant code:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure GetNextSourceLine(); { *** GETNEXTSOURCELINE  *** Identify the current SourceFile in the SourceFileTable  *** This is given by the global variable CurrentSource. The entry  *** in SourceFileTable will include not only the handle for the file,  *** but also the important information about the file that will make  *** it possible to know how to read the next line. See tSFTEntry=record } var  SourceFile    : THandle;  inputline     : string[MAXSOURCELINELENGTH];  {=120}  outputline    : string;  ReadLength    : integer;  ReadError     : integer;  msg           : string;begin   with SourceFileTable[CurrentSource] do       begin         SourceFile     := SFTFile;         ReadError     := 0;         {$i-}         if CurrentSource = 1 then            begin              ReadLength := FileRead                           (SourceFile, inputline, MAXSOURCELINELENGTH);            end         else            begin            ReadLength := FileRead                           (SourceFile, inputline, MAXSOURCELINELENGTH);            end;         ReadError     :=ioresult();        {$i+}          if ReadLength = -1 then          begin              msg  := 'Error in FileRead: ' + format('%-10d',[ReadError]);              write(msg);              readln()            end         else            begin              SFTLastLineNum        := SFTLastLineNum + 1;              SourceLineNum         := SFTLastLineNum;              SFTLastLinePos      := 0;              SFTRecNum             := SFTRecNum + 1;            SFTLastSourceLine     := inputline;            end;       end;   (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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i F:= Fi leCreate ( ' t e s t . dat ' ) ;I f F = -1 thenHalt ( 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. ---------------------------------------------------------------------- Josh · 2025-08-26 19:57 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 ---------------------------------------------------------------------- Nitorami · 2025-08-26 20:24 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 · 2025-08-26 20:58 Quote from: tfurnivall on August 26, 2025, 08:54:19 pmI 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.