block read error

喜夏-厌秋 提交于 2019-11-28 13:47:14

问题


Can anybody please explain me why I am hitting 'I/O error 998' in the below block read?

function ReadBiggerFile: string;
var
  biggerfile: file of char;
  BufArray: array [1 .. 4096] of char; // we will read 4 KB at a time
  nrcit, i: integer;
  sir, path: string;
begin
  path := ExtractFilePath(application.exename);
  assignfile(biggerfile, path + 'asd.txt');
  reset(biggerfile);
  repeat
    blockread(biggerfile, BufArray, SizeOf(BufArray), nrcit);
    for i := 1 to nrcit do
    begin
      sir := sir + BufArray[i];
      Form4.Memo1.Lines.Add(sir);
    end;
  until (nrcit = 0);
  closefile(biggerfile);
  ReadBiggerFile := sir;
end;

回答1:


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;



回答2:


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);


来源:https://stackoverflow.com/questions/9493522/block-read-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!