Recent

Author Topic: Using Assign function  (Read 3626 times)

Centauri

  • New Member
  • *
  • Posts: 25
Using Assign function
« on: March 10, 2020, 12:42:57 pm »
Lazarus newby here - did some playing around with an unfinished Delphi application 8 years ago, and have done no programming since, so VERY rusty on Pascal syntax etc. Revisiting the application rewriting it with Lazarus, so lots of research & scouring of examples as I go, however this one escapes me.

A simple routine to read a text file, the contents of which will be used to set the path to my dbase tables (in case the deployment doesn't allow the path I have chosen during design). I declare a variable as type TextFile :
Code: Pascal  [Select][+][-]
  1. var
  2.   filePath: TextFile;
and then assign the text file name to it:
Code: Pascal  [Select][+][-]
  1. Assign(filePath, 'dBpath.txt');
but the compiler throws up an error of "wrong number of parameters specified for call to "Assign".  All info I can find says it only needs the two parameters. Anyone shed any light?

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Using Assign function
« Reply #1 on: March 10, 2020, 12:51:58 pm »
Hi!

To avoid those mix-ups the assign to a file was changed in Delphi 1 ( ~ 1995) to

assignFile (afile, filename);

So the syntax is

assignFile (afile, filename);
rewrite (afile)  or reset(afile);
.....
closeFile(afile)


Winni

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • Former Delphi 1-7, 10.2 user
Re: Using Assign function
« Reply #2 on: March 10, 2020, 01:00:02 pm »
See this old thread for an explanation.

Thaddy

  • Hero Member
  • *****
  • Posts: 15980
  • Censorship about opinions does not belong here.
Re: Using Assign function
« Reply #3 on: March 10, 2020, 01:04:32 pm »
Code: Pascal  [Select][+][-]
  1. var
  2.   Filepath:text;
  3.   s:string;
  4. begin
  5.   Assign(filePath, 'dBpath.txt');
  6.   rewrite(filepath);
  7.   writeln(filepath,'test');
  8.   flush(filepath); // this depends on platform, do it anyway.
  9.   close(Filepath);
  10.   // Assign(filepath,'dBpath.txt'); // this is not necessary
  11.   reset(filepath);
  12.   readln(filepath,s);
  13.   writeln(s);
  14.   close(filepath);
  15. end.
Must indeed be the umptiest time I answered this. Read the manuals....
« Last Edit: March 10, 2020, 01:12:25 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

MarkMLl

  • Hero Member
  • *****
  • Posts: 7888
Re: Using Assign function
« Reply #4 on: March 10, 2020, 01:28:52 pm »
All info I can find says it only needs the two parameters. Anyone shed any light?

I think a fair question is "where have you been looking?" since if it was FPC or Lazarus documentation (i.e. as distinct from Turbo Pascal or something even older) a fix is in order.

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: 15980
  • Censorship about opinions does not belong here.
Re: Using Assign function
« Reply #5 on: March 10, 2020, 01:33:16 pm »
All info I can find says it only needs the two parameters. Anyone shed any light?

I think a fair question is "where have you been looking?" since if it was FPC or Lazarus documentation (i.e. as distinct from Turbo Pascal or something even older) a fix is in order.

MarkMLl
My example is standard pascal. You do not even need to declare a mode....

And - not to put too fine a point on it - test my code before you answer otherwise your response is useless... >:D
If I smell bad code it usually is bad code and that includes my own code.

Centauri

  • New Member
  • *
  • Posts: 25
Re: Using Assign function
« Reply #6 on: March 10, 2020, 01:39:42 pm »
Thankyou winni & trev - AssignFile & CloseFile did indeed resolve the issue. Very confusing, as although I did see AssignFile used in an example (as well as Assign in another), neither the Lazarus wiki or Free Pascal Reference guide has an entry for AssignFile .....

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Using Assign function
« Reply #7 on: March 10, 2020, 02:12:56 pm »
Hi!

To make things clear - there is "another" assign in Pascal:

Code: Pascal  [Select][+][-]
  1. var
  2. MyBitmap : TBitmap;
  3. Img : TImage;
  4.  
  5. ....
  6. MyBitmap.assign(Img.Picture.Bitmap);
  7. ...
  8.  
  9.  
Winni

MarkMLl

  • Hero Member
  • *****
  • Posts: 7888
Re: Using Assign function
« Reply #8 on: March 10, 2020, 02:40:05 pm »
All info I can find says it only needs the two parameters. Anyone shed any light?

I think a fair question is "where have you been looking?" since if it was FPC or Lazarus documentation (i.e. as distinct from Turbo Pascal or something even older) a fix is in order.

MarkMLl
My example is standard pascal. You do not even need to declare a mode....

And - not to put too fine a point on it - test my code before you answer otherwise your response is useless... >:D

Thaddy, why are you responding like that to my posting? It wasn't even directed at you, it was for OP to try to find out where he was still finding mention of Assign() in the context of file handling.

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

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Using Assign function
« Reply #9 on: March 10, 2020, 02:59:57 pm »
[...] it was for OP to try to find out where he was still finding mention of Assign() in the context of file handling.

My guess? In the RTL help, in the reference for unit System.

Note that file-handling Assign shoudn't be a problem unless one is calling it inside a method of class which itself has an Assign method. Which is most (all?) classes, hence the AssignFile alias. It's basically a question of scope.
« Last Edit: March 10, 2020, 03:03:45 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Using Assign function
« Reply #10 on: March 10, 2020, 03:32:13 pm »
Hi!

This might help Centauri with his question about ISO-Pascal.

And Thaddy against his life-threatening allergy with assignfile.

Code: Pascal  [Select][+][-]
  1. system.assign (afile,Filename);
  2. reset(afile);   { or rewrite }
  3. ....
  4. system.close (aFile);
  5.  

Winni

Thaddy

  • Hero Member
  • *****
  • Posts: 15980
  • Censorship about opinions does not belong here.
Re: Using Assign function
« Reply #11 on: March 10, 2020, 04:01:22 pm »
The difference between Assign and Assignfile is a: only uses the system unit and b: has some overloads in case you should want to allow for codepages. Otherwise it is simply an alias for Assign,
So it is not an allergy, but quite often you do not want or need AssignFile. It draws in the objpas unit (because of an object supporting mode) and you do not always want that. I hope that explains it.
Pascal does not know about AssignFile, Object Pascal does...
« Last Edit: March 10, 2020, 04:08:23 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5726
  • Compiler Developer
Re: Using Assign function
« Reply #12 on: March 11, 2020, 09:36:15 am »
Note that file-handling Assign shoudn't be a problem unless one is calling it inside a method of class which itself has an Assign method. Which is most (all?) classes, hence the AssignFile alias. It's basically a question of scope.

Only classes that derive from TPersistent contain the Assign method (and of course other, unrelated classes might).

 

TinyPortal © 2005-2018