[SOLVED] File Not Found by FPC Reset under WSL2 Ubuntu-20.04 on Windows 10 lazarus / 32-Linux ---------------------------------------------------------------------- MisterD · 2024-02-14 01:12 I'm running WSL2 with Ubuntu-20.04 on Windows 10. The exe named chap1 is in my ~/pascal dir along with input file mod.txt. When attempting to open mod.txt, it crashes with a file not found error on line 591 which is the Reset command (see Log of Run below). Prior to the Reset command, the exe displays the current dir as ~/pascal. What would cause the mod.txt file to not be found in that dir? I do have rw access to mod.txt -rw-r--r-- 1 dawsond dawsond 29 Jul 22  2016 mod.txt Relevant Source 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      BEGIN       infilename:=parseString;   (* parse filename parameter *)       Writeln ('Current Directory is : ',GetCurrentDir);       writeln(' Loading file : ',infilename);       Assign(infile,infilename);       RESET(infile);       readfile:=true;  (* set flag telling nextchar function to read from a file *)       if cmdnm = load then         echo:=true;     END;  Log of Run ~/pascal$ ./chap1 -> )load mod.txt Current Directory is : /home/dawsond/pascal  Loading file : mod.txt An unhandled exception occurred at $0000000000402994: EInOutError: File not found   $0000000000402994 line 591 of chap1.pas   $00000000004026D5 line 616 of chap1.pas   $00000000004025A3 line 633 of chap1.pas   $0000000000404EA5 line 1548 of chap1.pas Version Info Lazarus IDE is not installed. Ubuntu-20.04 WSL version: 2.0.14.0 Kernel version: 5.15.133.1-1 WSLg version: 1.0.59 MSRDC version: 1.2.4677 Direct3D version: 1.611.1-81528511 DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp Windows version: 10.0.19045.2913 Free Pascal Compiler version 3.0.4+dfsg-23 [2019/11/25] for x86_64 Attempted Solutions - prefixed filename with full path - added ~/pascal to $PATH ---------------------------------------------------------------------- dbannon · 2024-02-14 01:46 MisterD, maybe a good a good idea to test if the file really is there before opening it ? 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       if not FileExists(inifilename) then begin          writeln(ininfilename, ' does not seem to exist');          exit;      end       Assign(infile,infilename);       RESET(infile); Certainly seems to be there, but that assumes inifilename really does contain the right name and its accessible. Davo ---------------------------------------------------------------------- TRon · 2024-02-14 02:20 A couple of additional remarks: - If you use old-style file operations then make sure to always check ioresult manually, see also example in manual - Though your presented information do not seem to show any signs, realize that in Linux filenames and paths are case sensitive. - Seeing your end statement ending in a semicolon (and not a dot) I can only assume that the snippet posted is part of a bigger code-base and one of the things missing in the presented snippet is the actual closing of the opened file. Note that omission of closing an opened file can lead to filehandles staying open and most file-systems really do not like that, and which can result in erroneous behaviour. - Free Pascal 3.0.4 was released in nov 2017... almost 7 years ago. Please update. ---------------------------------------------------------------------- MisterD · 2024-02-14 04:10 Thank you Davo for your suggestion. The spaces between the filename and message in Log 1 below made me realize that I needed to pad infilename with trailing nulls instead of trailing spaces. Now it works correctly as shown in Log 2. TRon, thank you for your helpful comments as well. Log 1 - erroneous exe with trailing spaces in file name ~/pascal$ ./chap1 -> )load mod.txt Current Directory is : /home/dawsond/pascal  Loading file : mod.txt mod.txt                                                                        does not seem to exist -> quit$ Log 2 - corrected exe with trailing nulls in filename mod.txt is read and mod function is defined successfully ~/pascal$ ./chap1 -> )load mod.txt Current Directory is : /home/dawsond/pascal  Loading file : mod.txt mod.txt does exist  fun mod(m,n):=m-n*(m/n)nuf$ mod -> quit$ ---------------------------------------------------------------------- PascalDragon · 2024-02-14 19:52 Quote from: TRon on February 14, 2024, 03:20:12 am- If you use old- style file operations then make sure to always check ioresult manually, see also example in manual This is not necessary if the SysUtils is used as by default (unless $I- is used) an I/O error will be converted to an exception. See also here. ---------------------------------------------------------------------- TRon · 2024-02-15 10:28 Quote from: PascalDragon on February 14, 2024, 08:52:26 pmThis is not necessary if the SysUtils is used as by default (unless $I- is used) an I/O error will be converted to an exception. See also here. Thank you very much for the hint PascalDragon. Yes, that is definitely true but I made my remark with OP's code in mind which seem to require some form of proper file error handling before actually attempting to process the file. Though dbannon's suggestion is perhaps more elegant it is (theoretically) still possible that something is amiss when attempting to open/access the file. The exception is nice to have (as a backup to know what is/went wrong). @MisterD: I know you marked the issue as being solved but Quote from: MisterD on February 14, 2024, 05:10:26 amThank you Davo for your suggestion. The spaces between the filename and message in Log 1 below made me realize that I needed to pad infilename with trailing nulls instead of trailing spaces. .. When you write padding with trailing nulls then something tells me that there might be something wrong somewhere in your parsing routine. Are you using hard-coded length strings (shortstrings) or something ? And are you aware of the function trim that automatically strips non visible characters from a string  ? As a hint: when you write out filenames (but I personally also use it for displaying any string data) try to enclose the output into quotes (e.g. quotedstr) so that you are able to visually inspect the outputted string in a more detailed manner.