Recent

Author Topic: Compiler error when checking File Handle  (Read 730 times)

JohnnieK

  • New Member
  • *
  • Posts: 28
Compiler error when checking File Handle
« on: September 17, 2024, 02:38:30 pm »
Hi

I have the following snippet of code that compiles perfectly under Lazarus 2.2.6 with FPC 3.2.2

Code: Pascal  [Select][+][-]
  1.    
  2.     F := FileCreate(LogFileName);
  3.     if F=-1 then
  4.      Begin
  5.       Writeln('Unable to create log file: ',LogFileName);
  6.       Exit;
  7.      end;
  8.  

and according to the docs at https://www.freepascal.org/docs-html/rtl/sysutils/filecreate.html that is the correct way.

I have built Lazarus and FPC 3.3.1 from source about 2 weeks ago and with that combo I get the following error:

xxx.pas(869,10) Error: Range check error while evaluating constants (-1 must be between 0 and 18446744073709551615)

Did something change or is this a bug ?

440bx

  • Hero Member
  • *****
  • Posts: 4559
Re: Compiler error when checking File Handle
« Reply #1 on: September 17, 2024, 04:11:54 pm »
if "F" is an unsigned type then comparing it to -1 will cause range check error.

How did you define "F" ?
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

TRon

  • Hero Member
  • *****
  • Posts: 3283
Re: Compiler error when checking File Handle
« Reply #2 on: September 17, 2024, 04:16:08 pm »
Exactly as asked by 440bx.

the declaration seem to conveniently be missing from the example.

fwiw the more "correct" way of doing it is comparing against feInvalidHandle
This tagline is powered by AI

Zvoni

  • Hero Member
  • *****
  • Posts: 2692
Re: Compiler error when checking File Handle
« Reply #3 on: September 17, 2024, 04:22:43 pm »
The returntype of FileCreate is a THandle, which is a System.THandle, which is a Longint, which is - 2147483648..2147483647
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

BrunoK

  • Hero Member
  • *****
  • Posts: 589
  • Retired programmer
Re: Compiler error when checking File Handle
« Reply #4 on: September 17, 2024, 05:35:56 pm »
The returntype of FileCreate is a THandle, which is a System.THandle, which is a Longint, which is - 2147483648..2147483647
Dont know where you found that. Just search in files with Regular Expressions for " THandle[ ]*=", in the FPC directory rtl. There is quite a great variety of specifications.

Zvoni

  • Hero Member
  • *****
  • Posts: 2692
Re: Compiler error when checking File Handle
« Reply #5 on: September 17, 2024, 06:11:57 pm »
The returntype of FileCreate is a THandle, which is a System.THandle, which is a Longint, which is - 2147483648..2147483647
Dont know where you found that. Just search in files with Regular Expressions for " THandle[ ]*=", in the FPC directory rtl. There is quite a great variety of specifications.
See first post
https://www.freepascal.org/docs-html/rtl/sysutils/filecreate.html
« Last Edit: September 17, 2024, 06:13:48 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

BrunoK

  • Hero Member
  • *****
  • Posts: 589
  • Retired programmer
Re: Compiler error when checking File Handle
« Reply #6 on: September 17, 2024, 06:24:34 pm »
See first post
https://www.freepascal.org/docs-html/rtl/sysutils/filecreate.html
Yes the doc says THandle(-1) that is whatever is represented by not 0.

BrunoK

  • Hero Member
  • *****
  • Posts: 589
  • Retired programmer
Re: Compiler error when checking File Handle
« Reply #7 on: September 17, 2024, 06:25:47 pm »
Note the transtyping. It is not a comparison to -1.

TRon

  • Hero Member
  • *****
  • Posts: 3283
Re: Compiler error when checking File Handle
« Reply #8 on: September 17, 2024, 06:34:56 pm »
Note the transtyping. It is not a comparison to -1.
Probably my lack of English skills but it is literally done in the example. The problem is that the examples are stated to not have the need to be accurate and/or real world examples.

Also please note that the online documentation is generated on/for one specific platform.
« Last Edit: September 17, 2024, 06:36:57 pm by TRon »
This tagline is powered by AI

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1408
    • Lebeau Software
Re: Compiler error when checking File Handle
« Reply #9 on: September 17, 2024, 07:48:54 pm »
Code: Pascal  [Select][+][-]
  1.     F := FileCreate(LogFileName);
  2.     if F=-1 then

You need to type-cast the literal:

Code: Text  [Select][+][-]
  1. if F = THandle(-1) then

Or use the INVALID_HANDLE_VALUE constant:

Code: Text  [Select][+][-]
  1. if F = INVALID_HANDLE_VALUE then
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

ASerge

  • Hero Member
  • *****
  • Posts: 2324
Re: Compiler error when checking File Handle
« Reply #10 on: September 17, 2024, 08:57:07 pm »
The return type for FileCreate since Delphi 2010 has changed from an Integer type to THandle.
I don't remember which version of FPC this happened in. The example from the FPC documentation has remained since then.

af0815

  • Hero Member
  • *****
  • Posts: 1369
Re: Compiler error when checking File Handle
« Reply #11 on: September 18, 2024, 09:35:48 am »
About samples in the documentation

The samples contained in the documentation are not necessarily meant to be completely usable or even compileable. They simply illustrate the principles of what is documented.
regards
Andreas

TRon

  • Hero Member
  • *****
  • Posts: 3283
Re: Compiler error when checking File Handle
« Reply #12 on: September 18, 2024, 05:54:27 pm »
Or use the INVALID_HANDLE_VALUE constant:
Which seems to be a windows only solution.

Please use feInvalidHandle instead.

@af0815:
Thank you for that. That was one of the quotes I was referring to.
« Last Edit: September 18, 2024, 05:57:12 pm by TRon »
This tagline is powered by AI

Thaddy

  • Hero Member
  • *****
  • Posts: 15722
  • Censorship about opinions does not belong here.
Re: Compiler error when checking File Handle
« Reply #13 on: September 18, 2024, 07:11:38 pm »
Correct. But using filecreate() itself is alreafy a shortcut and nobody uses it - or should use it. What is wrong with rewrite()? Because that is the correct syntax to create a file.
If I smell bad code it usually is bad code and that includes my own code.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5686
  • Compiler Developer
Re: Compiler error when checking File Handle
« Reply #14 on: September 19, 2024, 09:37:17 pm »
But using filecreate() itself is alreafy a shortcut and nobody uses it - or should use it. What is wrong with rewrite()? Because that is the correct syntax to create a file.

FileCreate is simply the SysUtils style, cross platform file API. There is nothing wrong with using it. Rewrite requires the use of Pascal style I/O which might not be desired (the File*-APIs are more direct wrappers around the operating system's APIs than the Pascal style I/O, because there isn't any buffering in the way which might mess up things).

 

TinyPortal © 2005-2018