Forum > Linux

[SOLVED] File Not Found by FPC Reset under WSL2 Ubuntu-20.04 on Windows 10

(1/2) > >>

MisterD:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---     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:
MisterD, maybe a good a good idea to test if the file really is there before opening it ?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---      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:
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:
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:

--- 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
--- End quote ---

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.

Navigation

[0] Message Index

[#] Next page

Go to full version