Lazarus

Miscellaneous => Other => Topic started by: turunk on April 08, 2025, 11:42:54 pm

Title: Compile Error on Assign for Search Files
Post by: turunk 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.  
Title: Re: Compile Error on Assign for Search Files
Post by: speter 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.
Title: Re: Compile Error on Assign for Search Files
Post by: cdbc 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
Title: Re: Compile Error on Assign for Search Files
Post by: turunk 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.
Title: Re: Compile Error on Assign for Search Files
Post by: cdbc 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
Title: Re: Compile Error on Assign for Search Files
Post by: turunk on April 14, 2025, 08:02:41 pm
but the compiler points to the A of assign.
not the variable is the problem.
Title: Re: Compile Error on Assign for Search Files
Post by: cdbc 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
Title: Re: Compile Error on Assign for Search Files
Post by: TRon 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 (https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-win32_find_dataa).

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 (https://www.freepascal.org/docs-html/rtl/sysutils/findfirst.html) (scroll down) is a short example of how it should look like doing it as intended.
Title: Re: Compile Error on Assign for Search Files
Post by: cdbc 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
Title: Re: Compile Error on Assign for Search Files
Post by: Thaddy 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.
Title: Re: Compile Error on Assign for Search Files
Post by: TRon 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.
Title: Re: Compile Error on Assign for Search Files
Post by: Thaddy 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)
Title: Re: Compile Error on Assign for Search Files
Post by: TRon 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 ....  :)
Title: Re: Compile Error on Assign for Search Files
Post by: Thaddy 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.
Title: Re: Compile Error on Assign for Search Files
Post by: cdbc 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
Title: Re: Compile Error on Assign for Search Files
Post by: Thaddy on April 15, 2025, 01:13:31 pm
Benny, you are a kid compared to me  :'(
Title: Re: Compile Error on Assign for Search Files
Post by: cdbc on April 15, 2025, 01:17:08 pm
Oooohhhh Thaddy, you make me feel all warm and fuzzy  :D
Me likey, but I'm still snapping on your heels  ;D
Regards /ze youngsta/
Title: Re: Compile Error on Assign for Search Files
Post by: Khrys on April 15, 2025, 01:50:18 pm
Code: Pascal  [Select][+][-]
  1. // [...]
  2.   End Else Begin
  3.     AsSign(F3,Find.Name);
  4.     ReSet(F3);
  5.     Close(F3);
  6.   End;
  7. // [...]

It's been a while since the word boundary between ad- and signō disappeared (around 2000 years, give or take) - are you an ancient Roman by any chance?  ;)

Kidding aside, case insensitivity is my least favourite thing about Pascal. I really wish the following wouldn't compile:

Code: Pascal  [Select][+][-]
  1. funCtION fOo(cONSt bAR: stRing): STRing;
  2. bEGin
  3.   rEsulT := aNsiupPERcASe(baR);
  4. eNd;
Title: Re: Compile Error on Assign for Search Files
Post by: cdbc on April 15, 2025, 02:25:47 pm
Hi
@Khrys: I just rearranged TS code a bit, for readability, didn't do anything to the casing...  %)
Regards Benny
Title: Re: Compile Error on Assign for Search Files
Post by: ASerge on April 16, 2025, 02:58:48 am
If we ignore the fact that the code is meaningless, then in my opinion, in order to be correct, the code should be like this:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses SysUtils;
  4.  
  5. var
  6.   FA, FB, FOther: TextFile;
  7.   SearchRec: TSearchRec;
  8.   WhereToSearch: string;
  9. begin
  10.   AssignFile(FA, 'A.txt');
  11.   Reset(FA);
  12.   try
  13.     Readln(FA, WhereToSearch);
  14.   finally
  15.     CloseFile(FA);
  16.   end;
  17.   AssignFile(FB, 'B.txt');
  18.   Rewrite(FB);
  19.   try
  20.     if FindFirst(WhereToSearch, faAnyFile, SearchRec) = 0 then
  21.     try
  22.       repeat
  23.          AssignFile(FOther, SearchRec.Name);
  24.          if (SearchRec.Attr and faDirectory) = 0 then
  25.          begin
  26.            Reset(FOther);
  27.            try
  28.              // Do something with FB and FOther
  29.            finally
  30.              CloseFile(FOther);
  31.            end;
  32.          end;
  33.       until FindNext(SearchRec) <> 0;
  34.     finally
  35.       FindClose(SearchRec);
  36.     end;
  37.   finally
  38.     CloseFile(FB);
  39.   end;
  40. end.
Title: Re: Compile Error on Assign for Search Files
Post by: turunk on April 19, 2025, 04:19:49 pm
i use lazarus 3.8 and the dos unit seems as windows version and take longnames.
cFileName: array[0..MAX_PATH-1] of Char;
if i write 3.assign(F3,'C.txt'); it will success, but i need a variable variable set by reading.
Title: Re: Compile Error on Assign for Search Files
Post by: Thaddy on April 19, 2025, 04:27:02 pm
ASerge is as - almost - usual correct.
Title: Re: Compile Error on Assign for Search Files
Post by: marcov on April 20, 2025, 03:10:08 pm
In general avoid unit Dos, and use unit sysutils. I haven't used unit Dos in decades, other than the odd INTR() call when I still did (some) dos.
Title: Re: Compile Error on Assign for Search Files
Post by: turunk on April 25, 2025, 02:30:39 pm
i solved it so:
stringtypeof-assign:=valueofothervariable;
assign(f,stringtypeof-assign);
Title: Re: Compile Error on Assign for Search Files
Post by: turunk on May 04, 2025, 01:20:55 am
i have found not at sysutils the findfirst.
how findfirst with longnames and even path longer than 256 chars?
Title: Re: Compile Error on Assign for Search Files
Post by: Thaddy on May 04, 2025, 10:21:35 am
On windows put the find strings in double brackets for long filenames.
Fisrt single ' then ", so '"d:\somedir\a long filename"'
Title: Re: Compile Error on Assign for Search Files
Post by: bytebites on May 04, 2025, 12:15:44 pm
" is quotation mark not bracket.
Title: Re: Compile Error on Assign for Search Files
Post by: Thaddy on May 04, 2025, 05:32:04 pm
Yes I know. Tired.
Title: Re: Compile Error on Assign for Search Files
Post by: turunk on May 05, 2025, 03:10:06 pm
thaddy,
no i meant, if findfirst is not in sysutils, where i can find this procedure,
which searches longnames.

and the question: how is searchrec.twinfinddata.cfilename with 256 chars and searchrec.name a string,
if they are for 8.3
TinyPortal © 2005-2018