FileRead returns -1, but IOResult returns 0 (no error)? lazarus / 21-General ---------------------------------------------------------------------- tfurnivall · 2025-06-06 21:29 I am trying to manage a diverse set of files - whose structure may not be known until they are opened. To do this I have set up a table of files, as follows: 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 const { Managing the SourceFileTable}  MAXSFTENTRIES = 12;  MAXSOURCELINELENGTH = 120;  typetSFTEntry=record          SFTFile         : THandle;      { File number associated with entry}          SFTLastLineNum    : integer;          SFTLastLinePos    : integer;        SFTNumberedFile   : boolean;          SFTLineNumPos     : integer;      { Position & Length of any line- }          SFTLineNumLen     : integer;      { number for the file           }        SFTRecNum         : integer;          SFTCompressedText : boolean;      { File uses compressed text }          SFTUsage          : char;         { How the file is being used }          SFTModule       : alfa;         { Name of module being copied }          SFTName         : string;       {Name of file }          SFTLastSourceLine : string [MAXSOURCELINELENGTH]; {Last line read }          end;varCurrentSource : 1..MAXSFTENTRIES;SourceFileTable : array [1..MAXSFTENTRIES] of tSFTEntry;   I have opened the first file (no more than 11 left to go!) and am trying to read a line. I'm consistently getting a ReadLength of -1, but when I try and get the value of IOResult, it comes back as 0. (Which I'm taking as No Error). 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 goiven 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];  outputline    : string;  ReadLength    : integer;  msg           : string;begin   with SourceFileTable[CurrentSource] do       begin         SourceFile   := SFTFile;         ReadLength := FileRead (SourceFile, inputline, MAXSOURCELINELENGTH);          if ReadLength = -1 then            begin              msg  := 'Error in FileRead: ' + format('%-10d',[ioresult]);              write(msg);              readln()            end         else            begin              SFTLastLineNum        := SFTLastLineNum + 1;              SourceLineNum         := SFTLastLineNum;              SFTLastLinePos      := 0;              SFTRecNum             := SFTRecNum + 1;            SFTLastSourceLine     := inputline;            end;       end;end;  The file is opened using FileOpen, for read acccess. Can any of the gentle souls, who are so patient with newbies, provide me with any pointers (including RTFM if that's what it is!), Thanks in advance, Tony ---------------------------------------------------------------------- jamie · 2025-06-06 23:04 Does not like those functions uses the IORESULT variable to store the results. But looking at your code frag, my guess is that you attempted to read another block that isn't there, so that means at the end of file etc.  But since those look like an OS file function with a little alteration to make things easier, I think if you were on windows you can use "GetLastError"   why not use the Pascal IO which you can use the IOResults as a reference?  Also, you can use a TFIleStream of sorts or any component that has streaming file reading abilities. ---------------------------------------------------------------------- cdbc · 2025-06-07 05:45 Hi Try this: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 ReadLength := FileRead (SourceFile, inputline[1], MAXSOURCELINELENGTH);Regards Benny ---------------------------------------------------------------------- Remy Lebeau · 2025-06-07 06:25 Quote from: tfurnivall on June 06, 2025, 11:29:44 pmI'm consistently getting a ReadLength of -1, but when I try and get the value of IOResult, it comes back as 0. FileRead() is a SysUtils function, it doesn't use IOResult. That is used only by the System I/O functions mentioned in https://wiki.freepascal.org/File_Handling_In_Pascal. ---------------------------------------------------------------------- marcov · 2025-06-07 08:23 https://www.freepascal.org/docs-html/rtl/sysutils/getlastoserror.html ---------------------------------------------------------------------- jamie · 2025-06-07 13:55 The issue is that when looking at the Help for all the related functions, it does not show the "GetLastOSError" so it's hard to determine this at first.   I wasn't aware of this function existing. Jamie ---------------------------------------------------------------------- paule32 · 2025-06-07 15:11 for those that find it intresting: the status codes of Windows in a .mo and .po File + Python file to create the output data: https://kallup.net/win32/status.zip ---------------------------------------------------------------------- jamie · 2025-06-07 15:32 Quote from: cdbc on June 07, 2025, 07:45:29 amHi Try this: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 ReadLength := FileRead (SourceFile, inputline[1], MAXSOURCELINELENGTH);Regards Benny  You forgot to set the length!  Maybe this is better 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  InpintLine :Array[0..MAXSOURCELINELENGTH] of char;    Then you can address it without offsets. ---------------------------------------------------------------------- cdbc · 2025-06-07 21:50 Hi @jamie: he's declared it as a shortstring of 120 chars. Regards Benny ---------------------------------------------------------------------- jamie · 2025-06-07 21:58 Sure, however you never set the first byte of the short string so if you were to use that short string how does it know the length ? Jamie ---------------------------------------------------------------------- cdbc · 2025-06-08 13:03 Hi jamie Like this: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 const { Managing the SourceFileTable}  MAXSFTENTRIES = 12;  MAXSOURCELINELENGTH = 120;...var  ...  inputline     : string[MAXSOURCELINELENGTH];Back in the old TP-days, that meant inputline[0] was set to char#120  I'm not aware that has changed... Regards Benny ---------------------------------------------------------------------- marcov · 2025-06-08 13:10 Quote from: jamie on June 07, 2025, 03:55:12 pmThe issue is that when looking at the Help for all the related functions, it does not show the "GetLastOSError" so it's hard to determine this at first.   I wasn't aware of this function existing. Afaik fileread c.s are old Borland abstractions to handle the win16 to win32 transition. I never use them, I either use blockread or streams. ---------------------------------------------------------------------- Thaddy · 2025-06-08 16:03 About the same here. The remark by Remy is of importance there: FileRead is in sysutils  and that draws in SEH so you should handle errors with exceptions. If you don't want that, use assign, rewrite/reset. Almost nobody uses these functions and if they do they are really not familiar with Pascal well enough. .Either use streams, or use old school. We should deprecate all that. ---------------------------------------------------------------------- Remy Lebeau · 2025-06-08 21:06 Quote from: Thaddy on June 08, 2025, 06:03:44 pmFileRead is in sysutils  and that draws in SEH so you should handle errors with exceptions. Even though SysUtils is where a lot of exception handling originates, not everything in SysUtils uses exceptions. The file I/O routines certainly do not (unless FreePascal does something different than Delphi here). For example, TFileStream uses SysUtils file I/O routines internally and adds exceptions on top of them.