-3

I get an error:

unit1.pas(91,31) Error: Incompatible type for arg no. 1: Got "File Of Byte", expected "AnsiString"

My code:

var
    f : file of byte; 

...

AssignFile(f, FileName);
Reset(f);
try
  TotalBytes := FileSize(f); // line 93
finally
  CloseFile(f);
end;  

Can someone help me?

user5226582
  • 1,946
  • 1
  • 22
  • 37
Greg
  • 11
  • 3
  • 2
    There are several versions of `FileSize()` function, in `system` and `fileutil` units. Try to fully specify it, like `system.FileSize(f);` – Abelisto Dec 28 '17 at 14:54
  • And we are all still waiting for comments from those four users who anonymously downvoted this question. Hey! Do not hide your faces, share your points with us! :o) – Abelisto Jan 01 '18 at 19:48
  • Abelisto, thanks very much ! I wrote in Lazarus: var LocalFile : String; begin ... TotalBytes:=CheckFileSize(LocalFile); – Greg Jan 03 '18 at 08:16
  • Hope my comment and [Rudy's](https://stackoverflow.com/a/48013993/593144) answer was helpful? If not just mention why, if yes - accept the answer. – Abelisto Jan 04 '18 at 02:09

2 Answers2

1

As @Abelisto said, in Lazarus there are two functions FileSize, one in the System unit and one in the Lazarus unit fileutil.

The former takes a File as parameter, whereas the latter takes a string.

So if your code has fileutil in a uses clause, the one from that unit takes precedence over the one in System. That explains the error message.

You will have to fully qualify the call, so instead of a plain FileSize(f), use System.FileSize(f), or, alternatively, use FileSize(FileName) or fileutil.FileSize(FileName).

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
0

Line 91 appears to be

Reset(f);

so it is not clear why you include the comment about line 93.

However, if you are getting an error on Reset(f), the cause must be something you have not told us in your q. To see why, please follow the steps below carefully.

Note: The reason for basing the call to FileSize in my code on the copy of the compiled EXE is so that the file is guaranted to exist but is not the EXE itself, because when the EXE is running it cannot by opened in a shareable mode, so attempting to call Reset on it would fail.

  1. Compile (but do not run yet) the console app below.

  2. Copy the resulting exe to a file in the same directory, but with the extension '.BU' rather than '.EXE' is that attempting Reset on the EXE itself will result in a RunError(5), which means "Access denied", because when the EXE is opened by the OS it isn't opened in a shareable mode.

  3. Now run the app. It should correctly report the size of the .BU file.

  4. Assuming the EXE works as predicted, you need to identify where your error is coming from. My first guess would be that the instance of FileSize isn't the one in the System unit - my code calls System.FileSize to ensure that the correct instance of FileSize gets invoked. You can check that by changing your code to TotalBytes := System.FileSize(... - if the error goes away, you've found the cause.

Code:

program Files2;

{$mode objfpc}{$H+}

uses
  SysUtils;

var
  TotalBytes : Int64;
  f : file of byte;
  FileName : String;
begin
  FileName := ChangeFileExt(ParamStr(0), '.BU'); // get name of this app
  AssignFile(f, FileName);
  Reset(f);
  try
    TotalBytes := System.FileSize(f);
    writeln('Size of ', FileName, ' = ', TotalBytes, ' bytes');
    readln;
  finally
    CloseFile(f);
  end;
end.
MartynA
  • 30,454
  • 4
  • 32
  • 73
  • Run this program: Var F : File Of byte; L : File Of Longint; filename : String; begin { filename := 'C:\someone.jpg'; Assign (F, filename); Reset (F); Edit1.Text:= 'File size in bytes : ' + Inttostr(FileSize(F)); Close (F); – Greg Dec 28 '17 at 12:04
  • @Greg: I did, before I posted the updated version of this answer. – MartynA Dec 28 '17 at 20:12
  • FWIW, the line `Reset(f);` doesn't have 31 characters, so I assume the inline comment is wrong – Rudy Velthuis Dec 31 '17 at 20:42
  • @RudyVelthuis: TBH, given the lack of helpful information from, and zero effort by, the OP, I'm wondering why either of us bothered. – MartynA Dec 31 '17 at 20:48
  • @MartynA: yeah, I know what you mean. But it's a holiday in many countries, so who knows? – Rudy Velthuis Dec 31 '17 at 20:54
  • @ RudyVelthuis, @ MartynA. Please, waits for my comments and answers. I haven't no time to read Yours answers. In two days or a little more I answer. – Greg Jan 02 '18 at 20:36