Recent

Author Topic: Assign (textfile) not compiling - sometimes.  (Read 2178 times)

teresa

  • Guest
Assign (textfile) not compiling - sometimes.
« on: April 18, 2024, 01:44:26 am »
Is there a bug in text file handling?  FPC 3.2.2, Lazarus 3.2 - recently updated.

I have a few hundred lines of code. In the global variables there is
Code: Pascal  [Select][+][-]
  1.    {$DEFINE debug}
  2.  
  3.    {$IFDEF debug}
  4.    debugfile: Text;
  5.    {$ENDIF}
and later:
Code: Pascal  [Select][+][-]
  1.    {$IFDEF debug}
  2.    assign(debugfile, 'overfs.debug'); // problem here
  3.    rewrite(debugfile);
  4.    {$ENDIF}
(I'm doing it this way because I need to collect a whole lot of data - more than I want to see in the debugger.)
   
The editor is perfectly happy to suggest completions for the assign - for example suggesting Assign(out t: text; const c: shortstring);

But the compiler has other ideas:
   
Code: Pascal  [Select][+][-]
  1. unit1.pp(343,36) Error: (3026) Wrong number of parameters specified for call to "Assign"
  2. /usr/local/lib/fpc/3.2.2/units/x86_64-linux/rtl/classes.ppu:persist.inc(104,23) Error: (5088) Found declaration: Assign(TPersistent);

(That declaration is the first in the list offered by the code editor. but not the one for today.)

I have worked around this issue with a stringlist, but what is happening? Enquiring minds etc.

TRon

  • Hero Member
  • *****
  • Posts: 3615
Re: Assign (textfile) not compiling - sometimes.
« Reply #1 on: April 18, 2024, 01:48:13 am »
replace
Code: Pascal  [Select][+][-]
  1.    {$IFDEF debug}
  2.    assign(debugfile, 'overfs.debug'); // problem here
  3.    rewrite(debugfile);
  4.    {$ENDIF}
with
Code: Pascal  [Select][+][-]
  1.    {$IFDEF debug}
  2.    system.assign(debugfile, 'overfs.debug'); // problem here
  3.    rewrite(debugfile);
  4.    {$ENDIF}
Probably better to prefix all system related routines, just in case.

The issue is that the code is probably situated somewhere in a control/component/form and there (in that scope) assign means something else.
This tagline is powered by AI

teresa

  • Guest
Re: Assign (textfile) not compiling - sometimes.
« Reply #2 on: April 18, 2024, 01:56:13 am »
Thanks TRon - the code is  indeed in a button click event.

Good to be reminded!

bytebites

  • Hero Member
  • *****
  • Posts: 680
Re: Assign (textfile) not compiling - sometimes.
« Reply #3 on: April 18, 2024, 07:12:17 am »
Or use AssignFile and CloseFile procedures from the SysUtils.

MarkMLl

  • Hero Member
  • *****
  • Posts: 7990
Re: Assign (textfile) not compiling - sometimes.
« Reply #4 on: April 18, 2024, 08:18:31 am »
Or use AssignFile and CloseFile procedures from the SysUtils.

Curiously, I don't see those indexed in the RTL documentation.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

dsiders

  • Hero Member
  • *****
  • Posts: 1280
Re: Assign (textfile) not compiling - sometimes.
« Reply #5 on: April 18, 2024, 08:51:13 am »
Or use AssignFile and CloseFile procedures from the SysUtils.

Curiously, I don't see those indexed in the RTL documentation.

MarkMLl

None of the procedure or functions in objpas.pp are indexed or generated in the documentation. Probably because they're wrapped with $ifdefs that weren't defined when the docs were built. My guess.

[Edit]
And I just checked... they are present in the FPDoc xml file.
« Last Edit: April 18, 2024, 08:55:33 am by dsiders »
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

Thaddy

  • Hero Member
  • *****
  • Posts: 16132
  • Censorship about opinions does not belong here.
Re: Assign (textfile) not compiling - sometimes.
« Reply #6 on: April 18, 2024, 10:51:18 am »
Or use AssignFile and CloseFile procedures from the SysUtils.
Those two do not belong there. They are simple aliases to system.assign() and system.close().
Should never have been introduced unless Delphi has them.

Hiding them from being documented in a wood of ifdefs is actually a good idea.... O:-)  Brilliant!
« Last Edit: April 18, 2024, 10:59:38 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Bart

  • Hero Member
  • *****
  • Posts: 5465
    • Bart en Mariska's Webstek
Re: Assign (textfile) not compiling - sometimes.
« Reply #7 on: April 18, 2024, 11:35:37 am »
Those two do not belong there. They are simple aliases to system.assign() and system.close().
Should never have been introduced unless Delphi has them.
And of course Delphi has them, for the reason this thread makes ovious: if you use assign/close in a form's event, you'll get an error.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 16132
  • Censorship about opinions does not belong here.
Re: Assign (textfile) not compiling - sometimes.
« Reply #8 on: April 18, 2024, 12:18:09 pm »
That does not make these things any better. It is not Pascal.
If I smell bad code it usually is bad code and that includes my own code.

MarkMLl

  • Hero Member
  • *****
  • Posts: 7990
Re: Assign (textfile) not compiling - sometimes.
« Reply #9 on: April 18, 2024, 02:11:48 pm »
Frankly, I am disappointed that such a prominent... in fact vocal if not prolix... member of the community is so dismissive of a relatively inexperienced user's problem, particularly since almost everybody gets caught by this at some point.

Assign() and Close() are a big problem, and while Close() is most often misinterpreted to mean a graphical form Assign() is implemented by fundamental classes including TList and TStringlist.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 16132
  • Censorship about opinions does not belong here.
Re: Assign (textfile) not compiling - sometimes.
« Reply #10 on: April 18, 2024, 03:16:52 pm »
If it were a beginner I would rephrase it. The one that made the suggestion is no beginner.
Assign() and Close() existed before some idiots mixed up the language without thinking.
Fixing a symptom without analyzing the cause is plain ignorance.
And this is such a case that is not only obvious, but obnoxious. IOW toxic language pollution.
« Last Edit: April 18, 2024, 03:22:25 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5750
  • Compiler Developer
Re: Assign (textfile) not compiling - sometimes.
« Reply #11 on: April 18, 2024, 09:51:50 pm »
If it were a beginner I would rephrase it. The one that made the suggestion is no beginner.
Assign() and Close() existed before some idiots mixed up the language without thinking.
Fixing a symptom without analyzing the cause is plain ignorance.
And this is such a case that is not only obvious, but obnoxious. IOW toxic language pollution.

Complaining about it doesn't help that AssignFile and CloseFile are the solutions the developers of Delphi had come up with to use in context of working with VCL components. And thus one should suggest teresa to either use those or - as done by TRon - System.Assign and System.Close without doing an unrelevant rant about why the Delphi developers did what.

 

TinyPortal © 2005-2018