Recent

Author Topic: Why The Error?  (Read 4072 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Why The Error?
« on: December 22, 2018, 01:17:23 am »
I get an error the Assign(myfile, FileName); umain.pas(973,28) Error: Wrong number of parameters specified for call to "Assign"

Don't understand it.


Code: Pascal  [Select][+][-]
  1.  procedure TFMain.LoadAirportBoxText;
  2.   Var
  3.    FileName : String = 'F:\WTTraffic\AirportList.txt';
  4.    TxtRec   : String = '';
  5.    myfile : text;
  6.    filename, textTmp : String;
  7.  begin
  8.   FileName := 'F:\WTTraffic\AirportList.txt';
  9.   Assign (myfile, FileName);
  10.   Reset(myfile);
  11.   Repeat
  12.    Readln(myfile,TextTmp);
  13.  
  14.   // Process text here
  15.  
  16.    until eof(myfile);
  17.  end;    
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

creaothceann

  • Full Member
  • ***
  • Posts: 117
Re: Why The Error?
« Reply #1 on: December 22, 2018, 01:51:53 am »
Use AssignFile and CloseFile.

Or just TStringList.LoadFromFile (unless you know that it'll not fit into memory).

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Why The Error?
« Reply #2 on: December 22, 2018, 02:51:53 am »
 Yea, That worked so I just went with it.

Thanks
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Why The Error?
« Reply #3 on: December 22, 2018, 07:55:50 am »
system.assign would also have worked. The wrong assign is in scope, so explicit scope is necessary.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: Why The Error?
« Reply #4 on: December 22, 2018, 03:43:55 pm »
Scope is not the only problem. The use of the repeat loop is erroneous in this case, because it assumes in advance that the file always has at least one line. If the file is empty, your loop will be executed once, loading an empty string. In addition, CloseFile is missing.

Use while loop instead:

Code: Pascal  [Select][+][-]
  1. var
  2.   Input: Text;
  3.   Line: String;
  4. begin
  5.   AssignFile(Input, 'F:\WTTraffic\AirportList.txt');
  6.   Reset(Input);
  7.  
  8.   while not EoF(Input) do
  9.   begin
  10.     ReadLn(Input, Line);
  11.  
  12.     // process line here
  13.   end;
  14.  
  15.   CloseFile(Input);
  16. end;
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Why The Error?
« Reply #5 on: December 22, 2018, 04:13:12 pm »
Scope is not the only problem. The use of the repeat loop is erroneous in this case,
This is not correct. This will set an error code in IOResult in such a case and already on reset. Close is indeed missing.
I advice against AssignFile/CloseFile. They have no advantage over classic system.Assign other than pulling in unnecessary code. They simply call system.assign and system.close.
In that case a FileStream is a much better option.
« Last Edit: December 22, 2018, 07:26:50 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

benohb

  • Full Member
  • ***
  • Posts: 213
Re: Why The Error?
« Reply #6 on: December 22, 2018, 07:06:35 pm »
Use AssignFile and CloseFile.

Or just TStringList.LoadFromFile (unless you know that it'll not fit into memory).

I prefer TFileStream

Especially with large files so that the memory is not filled

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: Why The Error?
« Reply #7 on: December 23, 2018, 08:10:21 pm »
This is not correct. This will set an error code in IOResult in such a case and already on reset.

I tested both solutions (both loops) and in both cases, IOResult returns 0.

The difference is that the while loop does not make unnecessary iteration when the source file is empty, and so it does not require adding unnecessary code inside the loop to detect the void. If the input file is empty, the loop should be completely omitted.

Quote
In that case a FileStream is a much better option.

For whom? The questioner can not understand the use of basic instructions, so the more it can not deal with the extraction of lines using the FileStream, which is not a simple task.
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Why The Error?
« Reply #8 on: December 23, 2018, 08:23:04 pm »
Quote
In that case a FileStream is a much better option.

For whom? The questioner can not understand the use of basic instructions, so the more it can not deal with the extraction of lines using the FileStream, which is not a simple task.

There are other TStream decendants that make it easier, like p.e. the descendants of TTextStream; in this case, TFileReader seems particularly suitable.

Although you're right that one should at least understand the basics, it doesn't look to me as if the OP didn't: his errors are more those of a (relatively) new comer.
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.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Why The Error?
« Reply #9 on: December 23, 2018, 09:35:52 pm »
This is not correct. This will set an error code in IOResult in such a case and already on reset.

I tested both solutions (both loops) and in both cases, IOResult returns 0.

The difference is that the while loop does not make unnecessary iteration when the source file is empty, and so it does not require adding unnecessary code inside the loop to detect the void. If the input file is empty, the loop should be completely omitted.

Quote
In that case a FileStream is a much better option.

For whom? The questioner can not understand the use of basic instructions, so the more it can not deal with the extraction of lines using the FileStream, which is not a simple task.
No you did not test it. You did not test:
- a File does not exist (IOResult on reset)
- a File is empty (0) (IOResult same, on some platforms eof by defaultr)

Plz test something useful and plz listen to old hands. <Ok, for Christmas:  >:D >:D > Couldn't find the other red one..... Don't do that... >:( Merry Christmas!! :D :)
« Last Edit: December 23, 2018, 09:39:46 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: Why The Error?
« Reply #10 on: December 23, 2018, 10:28:58 pm »
No you did not test it. You did not test:
- a File does not exist (IOResult on reset)
- a File is empty (0) (IOResult same, on some platforms eof by defaultr)

Yes, I tested all of it. If a file is empty, IOResult returns 0 after Reset. If the source file not exists, IOResult returns nonzero error code (so it has to be), so entering the repeat loop and trying to read the data is pointless.

Code: Pascal  [Select][+][-]
  1. var
  2.   Input: Text;
  3.   Line: String;
  4. begin
  5.   {$I-}  
  6.  
  7.   Assign(Input, 'C:\file.txt');
  8.   Reset(Input);
  9.   WriteLn('Reset code: ', IOResult(), LineEnding);
  10.  
  11.   while not EoF(Input) do
  12.   begin
  13.     ReadLn(Input, Line);
  14.     WriteLn('"', Line, '"');
  15.   end;
  16.  
  17.   Close(Input);
  18.   WriteLn(LineEnding, 'Close code: ', IOResult());
  19. end.

The output is:

Code: Pascal  [Select][+][-]
  1. Reset code: 0
  2.  
  3.  
  4. Close code: 0

But with repeat loop:

Code: Pascal  [Select][+][-]
  1. var
  2.   Input: Text;
  3.   Line: String;
  4. begin
  5.   {$I-}  
  6.  
  7.   Assign(Input, 'C:\file.txt');
  8.   Reset(Input);
  9.   WriteLn('Reset code: ', IOResult(), LineEnding);
  10.  
  11.   repeat
  12.     ReadLn(Input, Line);
  13.     WriteLn('"', Line, '"');
  14.   until EoF(Input);
  15.  
  16.   Close(Input);
  17.   WriteLn(LineEnding, 'Close code: ', IOResult());
  18. end.

the output is:

Code: Pascal  [Select][+][-]
  1. Reset code: 0
  2.  
  3. ""
  4.  
  5. Close code: 0

As you can see, the loop iterate ones, loading and processing an empty string. But if the source file not exists, in both cases the output is the same:

Code: Pascal  [Select][+][-]
  1. 2
  2.  
  3. 103

Don't know why only the error codes, but it is not important now. Tested few minutes ago under Windows. So, if the repeat loop is recommended, how to detect the void? I'm asking seriously.

If I need to test if the file exists, I can use FileExists function. Or just check the IOResult:

Code: Pascal  [Select][+][-]
  1. {$I-}
  2.  
  3. Assign(Input, 'C:\file.txt');
  4. Reset(Input);
  5.  
  6. if IOResult() = 0 then // prevents reading from a non-existent file
  7. begin
  8.   while not EoF(Input) do // prevents reading from an empty file
  9.   begin
  10.     ReadLn(Input, Line);
  11.     WriteLn('"', Line, '"');
  12.   end;
  13.  
  14.   Close(Input); // closes the file only if it has been opened
  15. end;
« Last Edit: December 23, 2018, 10:39:11 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Why The Error?
« Reply #11 on: December 23, 2018, 10:36:52 pm »
Tested few minutes ago under Windows. As you can see, the loop iterate ones, loads an empty string. So, if the repeat loop is recommended, how to detect the void? I'm asking seriously.
Despite what Thaddy says about using the repeat loop I would also recommend the while loop. I've never seen a correct file handling routine using a repeat loop with eof(filehandle).

O, and BTW. Look at Thaddy's own example in this thread:
https://forum.lazarus.freepascal.org/index.php/topic,41726.msg290006.html#msg290006
He also uses while instead of repeat so he's just not understanding what you were meant to say with using "repeat is wrong". (Or he's just a bit confused)
« Last Edit: December 23, 2018, 10:42:47 pm by rvk »

furious programming

  • Hero Member
  • *****
  • Posts: 858
Re: Why The Error?
« Reply #12 on: December 23, 2018, 10:42:22 pm »
I have never used the repeat loop to read the contents of files and I never recommend using it. First, check whether the file contains data, and then try to load them. I encourage everyone to test the examples given by me in an earlier post, if my words seem unbelievable.

@lucamar: of course there are several different methods and streams, but here the FileStream has been specifically suggested, which is bad for OP.

@rvk++
« Last Edit: December 23, 2018, 10:51:01 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Why The Error?
« Reply #13 on: December 24, 2018, 06:05:47 am »
 I changed yo the "while not EoF(Input) do" here and several other places.

Makes for better code I can see that.

It is very difficult to know what you don't know.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Why The Error?
« Reply #14 on: December 24, 2018, 07:00:27 am »
It is very difficult to know what you don't know.

Everyone has passed for this phase. It (and you) gets better with practice, a lot of reading and looking to other people's code ;D
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.

 

TinyPortal © 2005-2018