Recent

Author Topic: Land of Confusion  (Read 10761 times)

Trunkles

  • New Member
  • *
  • Posts: 32
  • Making your screen look tatty.
Land of Confusion
« on: January 27, 2011, 06:43:11 am »
Hi all.

Here's the code.
Code: [Select]
Procedure TTail.DoTheTail;
var
   InFile, Outfile: Text;
   TextRead: array of String;
   TextLine: String;
   i: integer;

begin
     // Set the size of TextRead;
     SetLength(TextRead, LineCount);
     // Open the two files
     Assign(InFile, '');
     ...

And the compiler tells me...

Code: [Select]
Error: Wrong number of parameters specified for call to "Assign"
Er... From what I can see in rtl help one of the overloaded versions of assign() is...

Code: [Select]
procedure Assign(
  out t: Text;
  const s: String
);

So, folks, what the $^&*&%* have I misunderstood?
 
Simon.
Author of crap (and not so crap) software for over 30 years.

cdbc

  • Hero Member
  • *****
  • Posts: 2600
    • http://www.cdbc.dk
Re: Land of Confusion
« Reply #1 on: January 27, 2011, 07:52:35 am »
Hi
Code: [Select]
Procedure TTail.DoTheTail;
var
   InFile: Text;
   TextRead: array of String;
   TextLine: String;
   i: integer;
begin
  assign(InFile,'TailData.txt'); { assign filename to filehandle }
  try
    reset(InFIle); { open file for reading }
    I:= 0;
    while not EOF(InFile) do begin
      readln(InFile,TextLine); { read one line at the time }
      SetLength(TextRead,I+1); { reallocate buffer array }
      TextRead[I]:= TextLine;
      inc(I); { increment linecounter }
    end;
    ...
    { process buffer array }
    ...
  except on E:Exception do
    writeLOG('Error opening file: '+E.Message); { just in case }
  end;
  close(InFile);
end;

{ Tip:
  var List: TStrings;
  begin
    List:= TStringList.Create;
    List.Loadfromfile('TailData.txt');
    { process listdata }
    List.Free;
  end; }


hth
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

Trunkles

  • New Member
  • *
  • Posts: 32
  • Making your screen look tatty.
Re: Land of Confusion
« Reply #2 on: January 27, 2011, 08:02:32 am »
But that's what I've got in my code! I get the same error whether the file name is a null string or a value picked of the command line (It's a console app).
Author of crap (and not so crap) software for over 30 years.

cdbc

  • Hero Member
  • *****
  • Posts: 2600
    • http://www.cdbc.dk
Re: Land of Confusion
« Reply #3 on: January 27, 2011, 08:30:59 am »
Hi

My bad...
system.assign

hth 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

Trunkles

  • New Member
  • *
  • Posts: 32
  • Making your screen look tatty.
Re: Land of Confusion
« Reply #4 on: January 27, 2011, 09:13:29 am »
Now I understand.  :D

Thanks Benny, it all makes sense now.
Author of crap (and not so crap) software for over 30 years.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Land of Confusion
« Reply #5 on: January 27, 2011, 01:14:45 pm »
Or you can use AssignFile (and corresponding CloseFile). This is due to TForm has both Assign (derived from TComponent) and Close method.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Land of Confusion
« Reply #6 on: January 27, 2011, 01:23:06 pm »
Or you can use TStringList or TFileStream and get out of this land of confusion.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12634
  • FPC developer.
Re: Land of Confusion
« Reply #7 on: January 27, 2011, 01:41:09 pm »
Or you can use TStringList or TFileStream and get out of this land of confusion.

These are totally different approaches with many complicated tradeoffs. (e.g. tstringlist using three times the memory of the textfile, which unsuitable e.g. for database exports, or both not supporting esay writeln syntax)

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Land of Confusion
« Reply #8 on: January 27, 2011, 01:48:04 pm »
It is preferable to use a TStringList for the small files and TFileStream for the rest. I have posted a text file stream class here with a WriteLn method.

motaz

  • Sr. Member
  • ****
  • Posts: 495
    • http://code.sd
Re: Land of Confusion
« Reply #9 on: January 27, 2011, 01:50:05 pm »
I use AssignFile and CloseFile

btw. there is the same problem when using Delete procedure for string part deletion, we should use System.Delete, is there another procedure name for Delete?
« Last Edit: January 27, 2011, 01:55:51 pm by motaz »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12634
  • FPC developer.
Re: Land of Confusion
« Reply #10 on: January 27, 2011, 01:53:25 pm »
It is preferable to use a TStringList for the small files and TFileStream for the rest. I have posted a text file stream class here with a WriteLn method.

I use tstringlist too, but only for files that never will be large, and if I use tstringlist features like commatext or so. I don't see why it is "preferable"

I only use TFilestream (and streams in general) if I really use the kind of features that streaming adds (e.g. pass it around to functions that are agnostic if it is memory or file, or layer multiple levels of streams). I never do it by default, since the syntax is more involved and the read/write commands are less comfortable (specially for textfiles)

I haven't seen your stream class, but I doubt it works like normal write(ln) since that features several compiler magic bits (like the fieldwidth :x:y) that can't be duplicated with normal procedures or methods. And that (specially the fieldwidth) is what I like so much.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Land of Confusion
« Reply #11 on: January 27, 2011, 01:57:57 pm »
« Last Edit: January 27, 2011, 02:04:49 pm by typo »

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Land of Confusion
« Reply #12 on: January 27, 2011, 02:22:04 pm »
I have just updated the file, so please download it again now.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Land of Confusion
« Reply #13 on: January 27, 2011, 11:34:26 pm »
Quote
there is the same problem when using Delete procedure for string part deletion, we should use System.Delete, is there another procedure name for Delete?
AFAIK no. Just type and Ctrl+Click to know it.

 

TinyPortal © 2005-2018