block read error

爷,独闯天下 提交于 2019-11-29 18:02:26

I think you miss-tagged the question and you're using Delphi 2009+, not Delphi 7. I got the error in the title bar trying your exact code on Delphi 2010 (unicode Delphi). When you say:

var biggerfile: file of Char;

You're declaring the biggerfile to be a file of "records", where each record is a Char. On Unicode Delphi that's 2 bytes. You later request to read SizeOf(BufArray) records, not bytes. That is, you request to 4096 x 2 = 8192 records. But your buffer is only 4096 records long, so you get a weird error.

I was able to fix your code by simply replacing Char with AnsiChar, since AnsiChar has a size of 1, hence the SizeOf() equals Length().

The permanent fix should involve moving from the very old Pascal-style file operations to something modern, TStream based. I'm not sure exactly what you're trying to obtain, but if you simply want to get the content of the file in a string, may I suggest something like this:

function ReadBiggerFile: AnsiString;
var
  biggerfile: TFileStream;
begin
  biggerfile := TFileStream.Create('C:\Users\Cosmin Prund\Downloads\AppWaveInstall201_385.exe', fmOpenRead or fmShareDenyWrite);
  try
    SetLength(Result, biggerfile.Size);
    biggerfile.Read(Result[1], biggerfile.Size);
  finally biggerfile.Free;
  end;
end;

Hi: I had the same issue and i simply passed it the first element of the buffer which is the starting point for the memory block like so:

    AssignFile(BinFile,binFileName);
    reset(BinFile,sizeof(Double));
    Aux:=length(numberArray);
    blockread(BinFile,numberArray[0],Aux, numRead);
    closefile(BinFile);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!