block read error

前端 未结 2 1759
北恋
北恋 2020-12-22 12:06

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;
           


        
相关标签:
2条回答
  • 2020-12-22 12:48

    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);
    
    0 讨论(0)
  • 2020-12-22 12:57

    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;
    
    0 讨论(0)
提交回复
热议问题