Recent

Author Topic: Compile Error on Assign for Search Files  (Read 3418 times)

turunk

  • New Member
  • *
  • Posts: 29
Compile Error on Assign for Search Files
« on: April 08, 2025, 11:42:54 pm »
i wanted to search with files, but i got the error "Finderror1.lpr(20,18) Error: Can't determine which overloaded function to call" ,
2 times assign had no given error but then he gave this error, see:

Code: Pascal  [Select][+][-]
  1. Program Finderror1;
  2.  
  3.   Uses Dos;
  4.  
  5.   Var F1,F2:Text;
  6.     F3:Text;
  7.     T1:String;
  8.     Find:SearchRec;
  9.     FA:Byte;
  10.  
  11. Begin
  12.   AsSign(F1,'A.Txt');ReSet(F1);
  13.   ReadLn(F1,T1);
  14.   AsSign(F2,'B.Txt');ReWrite(F2);
  15.   FindFirst(T1,$3F,Find);
  16.   Repeat
  17.     If FA=0
  18.       Then Begin WriteLn(F2,'FA is 0');
  19.                  FindNext(Find);End
  20.       Else Begin AsSign(F3,Find.WinFindData.cFileName);
  21.                  ReSet(F3);
  22.                  Close(F3);
  23.     End;
  24.   Until Dos.DosError=18;
  25.   Close(F1);Close(F2);
  26.   FindClose(Find);
  27. End.
  28.  

speter

  • Sr. Member
  • ****
  • Posts: 487
Re: Compile Error on Assign for Search Files
« Reply #1 on: April 09, 2025, 12:01:45 am »
G'Day turunk,

1) Variable "FA" is not initialised. Did you mean to write:
Code: Pascal  [Select][+][-]
  1. FA := FindFirst(T1,$3F,Find);
2) With SearchRec type variables, I use find.filename for the matching filename...!?

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

cdbc

  • Hero Member
  • *****
  • Posts: 2603
    • http://www.cdbc.dk
Re: Compile Error on Assign for Search Files
« Reply #2 on: April 09, 2025, 12:45:55 am »
Hi
These 2 DON'T MIX: "Find.WinFindData.cFileName" & "Find:SearchRec;"
Instead you should use 'Find.Name'.
Look in the FPC Wiki or documentation, for examples on how to do the search properly...
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

turunk

  • New Member
  • *
  • Posts: 29
Re: Compile Error on Assign for Search Files
« Reply #3 on: April 09, 2025, 07:49:53 am »
no, the problem is the 3rd assign.
fa has not to be initialized.
if i compile he makes no problem at 1st and 2nd assign.
Find.WinFindData.cFileName is for the long names.

cdbc

  • Hero Member
  • *****
  • Posts: 2603
    • http://www.cdbc.dk
Re: Compile Error on Assign for Search Files
« Reply #4 on: April 09, 2025, 08:15:47 am »
Hi
This compiles for me (on Linux):
Code: Pascal  [Select][+][-]
  1. program find_errors;
  2.  
  3.   Uses Dos;
  4.  
  5.   Var F1,F2:Text;
  6.     F3:Text;
  7.     T1:String;
  8.     Find:SearchRec;
  9.     FA:Byte;
  10.  
  11. begin
  12.   Assign(F1,'A.Txt'); Reset(F1);
  13.   ReadLn(F1,T1);
  14.   AsSign(F2,'B.Txt');ReWrite(F2);
  15.   FindFirst(T1,$3F,Find);
  16.   Repeat
  17.     If FA=0
  18.       Then Begin WriteLn(F2,'FA is 0');
  19.                  FindNext(Find);End
  20.       Else Begin AsSign(F3,Find.Name); //<-- ONLY CHANGE!
  21.                  ReSet(F3);
  22.                  Close(F3);
  23.     End;
  24.   Until Dos.DosError=18;
  25.   Close(F1);Close(F2);
  26.   FindClose(Find);
  27. end.
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

turunk

  • New Member
  • *
  • Posts: 29
Re: Compile Error on Assign for Search Files
« Reply #5 on: April 14, 2025, 08:02:41 pm »
but the compiler points to the A of assign.
not the variable is the problem.

cdbc

  • Hero Member
  • *****
  • Posts: 2603
    • http://www.cdbc.dk
Re: Compile Error on Assign for Search Files
« Reply #6 on: April 14, 2025, 08:22:59 pm »
Hi
Assign has many overloads, so we need to know the exact error-message and what type "Find.WinFindData.cFileName" is?!?
Is it:
- array[0..255] of char
- pchar
- pwidechar
- shortstring
- string (ansi / rawbytestring)
- widestring
- unicodestring

Note: I have no winders to test against/on... so you're up.
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

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Compile Error on Assign for Search Files
« Reply #7 on: April 14, 2025, 08:33:22 pm »
Assign has many overloads, so we need to know the exact error-message and what type "Find.WinFindData.cFileName" is?!?
Windows.

Also note the usage of the absolete dos unit.

This is not the way to search for files unless doing it for windows only and in which case all these pascal encapsulation is redundant and counterproductive.

2 cents.

@turunk:
Here (scroll down) is a short example of how it should look like doing it as intended.
« Last Edit: April 14, 2025, 08:38:26 pm by TRon »
Today is tomorrow's yesterday.

cdbc

  • Hero Member
  • *****
  • Posts: 2603
    • http://www.cdbc.dk
Re: Compile Error on Assign for Search Files
« Reply #8 on: April 14, 2025, 08:58:49 pm »
Hi TRon
Oddly enough, this works like a charm on Linux:
Code: Pascal  [Select][+][-]
  1. program find_errors;
  2.  
  3.   Uses Dos;
  4.  
  5.   Var F1,F2:Text;
  6.     F3:Text;
  7.     T1:String;
  8.     Find:SearchRec;
  9.     FA: SmallInt;
  10.     iores: word;
  11.  
  12. begin
  13.   Assign(F1,'a.txt'); Reset(F1);
  14.   ReadLn(F1,T1);
  15.   AsSign(F2,'b.txt');ReWrite(F2);
  16.   FindFirst(T1,$3F,Find);
  17.   Repeat
  18.     FA:= DosError;
  19.     If FA = 0 Then Begin
  20.       writeln(f2,'FA is 0 - ',Find.Name);
  21.       FindNext(Find);
  22.     End Else Begin
  23.       AsSign(F3,Find.Name);
  24.       ReSet(F3);
  25.       Close(F3);
  26.     End;
  27.   Until Dos.DosError=18;
  28.   Close(F1);Close(F2);
  29.   FindClose(Find);
  30. end.  
...don't ask me to explain why  ;D
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

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Compile Error on Assign for Search Files
« Reply #9 on: April 14, 2025, 10:22:07 pm »
Well, he should not have used the dos unit.
On Windows it is a bit like this, on Linux it is as you wrote:
Code: Pascal  [Select][+][-]
  1. program find_errors;
  2. {$H-}{$I-}
  3. uses sysutils;
  4. Var
  5.  F1,F2,F3:Text;
  6.  T1:String;
  7.  Find:TSearchRec;
  8.  FA: SmallInt;
  9. begin
  10.   ExceptProc := nil;// disable SEH exceptions!!
  11.   Assign(F1,'a.txt'); Reset(F1);
  12.   ReadLn(F1,T1);
  13.   AsSign(F2,'b.txt');ReWrite(F2);
  14.   FindFirst(T1,$3F,Find);
  15.   Repeat
  16.     FA:= IOResult;
  17.     If FA = 0 Then Begin
  18.       writeln(f2,'FA is 0 - ',Find.Name);
  19.       FindNext(Find);
  20.     End Else Begin
  21.       AsSign(F3,Find.Name);
  22.       ReSet(F3);
  23.       Close(F3);
  24.     End;
  25.   Until IOResult = 0;// 18 is wrong
  26.   Close(F1);Close(F2);
  27.   FindClose(Find);
  28. end.  
BTW that also compiles for linux...
It is also quirky, since sysutils is drawn in exceptions are pulled in so for this code you could not use IOResult error handling. In the above code I simply disabled SEH exceptions.
« Last Edit: April 14, 2025, 10:51:02 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Compile Error on Assign for Search Files
« Reply #10 on: April 14, 2025, 10:48:50 pm »
Hi cdbc,

Oddly enough, this works like a charm on Linux:
It does, this is just the most cringe code I've seen in ages.

No idea what TS is trying to accomplish but...
Code: Pascal  [Select][+][-]
  1. program finder;
  2.  
  3. {$mode objfpc}{$h+}
  4.  
  5. uses
  6.   classes, Sysutils;
  7.  
  8. procedure Foo; begin end;
  9. procedure Bar; begin end;
  10.  
  11. var
  12.   Filenames: TStringList;
  13.   Filename : string;
  14. begin
  15.   Filenames := TStringList.Create;
  16.   Filenames.LoadFromFile('a.txt');
  17.  
  18.   for Filename in Filenames do
  19.     if FileExists(Filename)
  20.      then Foo
  21.       else Bar;
  22.   Filenames.Free;
  23. end.
  24.  

fwiw it amazes me that TS was able to compile that code with not including a windows unit of some sorts. Apparently we like to fish (hint: no, we don't).

edit: forgot to copy-paste the line that actually loads the list of filenames. Corrected now.
« Last Edit: April 14, 2025, 11:09:52 pm by TRon »
Today is tomorrow's yesterday.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Compile Error on Assign for Search Files
« Reply #11 on: April 14, 2025, 10:53:11 pm »
I certainly could not get that original code to work on windows because here on win64 I get a file not found error for the dos unit.
And if he is on linux the change from cdbc is correct:Find.WinFindData.cFileName does not exist. find.name does.
(It seems old TP code)
« Last Edit: April 14, 2025, 10:59:35 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Compile Error on Assign for Search Files
« Reply #12 on: April 14, 2025, 11:14:54 pm »
@Thaddy:
Pretty sure it is old tp code but referring to undefined types in example code used to depict an error is simply a fishing expedition. Nice if you have the time for such otherwise ....  :)
Today is tomorrow's yesterday.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Compile Error on Assign for Search Files
« Reply #13 on: April 15, 2025, 09:20:04 am »
no, the problem is the 3rd assign.
fa has not to be initialized.
if i compile he makes no problem at 1st and 2nd assign.
Find.WinFindData.cFileName is for the long names.
The DOS searchrecord does not support long filenames. Never has.
Either you use my code or you need to rewrite the application such that it uses the low level windows search API.
Btw, that is the only place your effort errs, nothing to do with the file handles.
To put it simple: the dos unit is not for windows and it is made invisible for windows.
That old TP code may compile on windows but not with long filenames. For that, the more "modern" file search API is needed (well that is "only" 35 years old, 1990) as is implemented in sysutils.
« Last Edit: April 15, 2025, 09:23:14 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

cdbc

  • Hero Member
  • *****
  • Posts: 2603
    • http://www.cdbc.dk
Re: Compile Error on Assign for Search Files
« Reply #14 on: April 15, 2025, 10:26:19 am »
Hi
If I'd have to hazard a guess  ...It would put the original code from TS, in the pre-TP5.5 era, most likely 4.0  %)  Just after the introduction of the 'Unit'-system.
Pretty good memory for such an old fart, like me  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