Recent

Author Topic: FPC says file exists, Lazarus says it doesn't. Same code.  (Read 921 times)

fxeconomist

  • Jr. Member
  • **
  • Posts: 67
FPC says file exists, Lazarus says it doesn't. Same code.
« on: March 05, 2026, 09:44:54 am »
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. function FileExists(fil:string):boolean;
  4. var f:file;resulted:boolean;
  5. begin
  6.   assign(f,fil);
  7.   {$I-}
  8.   reset(f);
  9.   {$I+}
  10.   if IOResult=0 then resulted:=TRUE
  11.                 else resulted:=FALSE;
  12.   if resulted=true then close(f);
  13.   FileExists:=resulted;
  14. end;
  15.  

Now, it doesn't say that all don't exist.
The files for which I believe it crashes are on a fullpath longer than 128 characters, because files with a shorter name in that folder get a pass.
Now I knew the limit was 255...
« Last Edit: March 05, 2026, 09:51:47 am by fxeconomist »

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: FPC says file exists, Lazarus says it doesn't. Same code.
« Reply #1 on: March 05, 2026, 10:22:43 am »
Do you get the same results with FileExists(...) in unit FileUtil?
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

fxeconomist

  • Jr. Member
  • **
  • Posts: 67
Re: FPC says file exists, Lazarus says it doesn't. Same code.
« Reply #2 on: March 05, 2026, 11:07:03 am »
Do you get the same results with FileExists(...) in unit FileUtil?

Oh, so FileUtil is a unit ? Indeed the documentation referred to something like that. I've seen include (*.inc) files related to lazutils. I actually put both other ones in the uses clause (that is, lazutils and lazfileutils as I also use FileGetAttrUTF8) but I found that the other FileExists is found in SysUtils. So, I shortcircuited my code, and now it works in Lazarus as well:

Code: Pascal  [Select][+][-]
  1. function FileExists(fil:string):boolean;
  2. var f:file;resulted:boolean;
  3. begin
  4.   {$IFNDEF CONSOLE}
  5.    FileExists:=sysutils.FileExists(fil,True);
  6.   {$ELSE}
  7.     assign(f,fil);
  8.     {$I-}
  9.     reset(f);
  10.     {$I+}
  11.     if IOResult=0 then resulted:=TRUE
  12.                   else resulted:=FALSE;
  13.     if resulted=true then close(f);
  14.     FileExists:=resulted;
  15.   {$ENDIF}
  16. end;
  17.  


JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4710
  • I like bugs.
Re: FPC says file exists, Lazarus says it doesn't. Same code.
« Reply #3 on: March 05, 2026, 11:19:41 am »
It makes no sense. Sysutils.FileExists can be used in a console program, as well as in a GUI application.
Quote
... as I also use FileGetAttrUTF8
In general you don't need the special ...UTF8() named functions when using the default UTF-8 encoding. They are still needed when a locale codepage encoding is used. It is a historical remain but some projects still must use it.
UTF8ToSys() is then called in those functions.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: FPC says file exists, Lazarus says it doesn't. Same code.
« Reply #4 on: March 05, 2026, 11:55:04 am »
You'll be quite busy if you want to replace all the standard, working(!) functions with your own custom ones  :D
Like Juha said, for these core functions/units it doesn't matter whether you write GUI or Console programs.
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

fxeconomist

  • Jr. Member
  • **
  • Posts: 67
Re: FPC says file exists, Lazarus says it doesn't. Same code.
« Reply #5 on: March 05, 2026, 04:57:18 pm »
You'll be quite busy if you want to replace all the standard, working(!) functions with your own custom ones  :D
Like Juha said, for these core functions/units it doesn't matter whether you write GUI or Console programs.

My code predates the "standard". I wrote it in BP7 initially...

cdbc

  • Hero Member
  • *****
  • Posts: 2726
    • http://www.cdbc.dk
Re: FPC says file exists, Lazarus says it doesn't. Same code.
« Reply #6 on: March 05, 2026, 05:06:05 pm »
Hi
Quote
My code predates the "standard". I wrote it in BP7 initially...
...Then it is _long_ overdue, to refactor your code to today's modern standards  ;D
I'll bet your units also uses "shortstring"s by default...
· StrUtils, FileUtils, SysUtils, DateUtils  ...actually the entire RTL are your friends!
Happy refactoring  8)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

 

TinyPortal © 2005-2018