gpt4 book ai didi

windows - 调用 FileRead 后的奇怪计数值

转载 作者:可可西里 更新时间:2023-11-01 11:23:59 24 4
gpt4 key购买 nike

我正在尝试将读取的字节数与传递给 FileRead 的计数进行比较这是 WinAPI 的包装器 ReadFile功能。

问题是我根据 ReadFromFile 过程的结构得到了不同的值(添加/减去的行都没有改变计数变量)。

如果你运行下面的代码,你会得到这个输出

FileHandle: 400
SizeOfFile: 8672
Current position: 8655
aCount before SetLength: 17
aCount before FileRead: 17
Number of bytes read: 17
aCount after FileRead: 2200
EAccessViolation: Access violation at address 0040C5BC in module 'Project7.exe'. Read of address C23C30BA

AV 是因为编译器在范围末尾释放了动态数组(并非总是如此)。正如您在 FileRead 之后看到的 count == 2200 (在此之前我得到的是 0)。如果您注释掉第二个 API 调用或第二行,则计数是正确的

你能告诉我这是什么吗?我该如何解决?

program Project7;

{$APPTYPE CONSOLE}

{$R *.res}

uses
System.SysUtils, WinAPI.Windows, System.Classes;


procedure ReadFromFile(aFileHandle: THandle; aCount: Longint);
var
aPosition, ReadRes: Int64;
TmpBuffer: TBytes;
begin
writeln('aCount before SetLength: ',aCount);
SetLength(TmpBuffer, aCount);
writeln('aCount before FileRead: ',aCount);
ReadRes := FileRead(aFileHandle, TmpBuffer, aCount);
Writeln('Number of bytes read: ', ReadRes);
//aPosition := FileSeek(aFileHandle, 0, Ord(soCurrent)); // second API call
//Writeln('Current position after read: ', aPosition); // line two
writeln('aCount after FileRead: ',aCount);
if ReadRes <> aCount then
//Raise Exception.Create('hi there');
// DoWrite(TmpBuffer[0], aCount);
end;

var
FFileHandle: THandle;
aFileName: string;
I1: Integer;
aFilePhysicalSize: Int64;
FPosition: Int64;
begin
try
aFileName := 'C:\Users\nacereddine\Desktop\ascii-table.gif';
{ TODO -oUser -cConsole Main : Insert code here }
FFileHandle := CreateFile(PChar(aFileName), GENERIC_READ,
FILE_SHARE_READ, nil, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
Writeln('FileHandle: ', FFileHandle);
aFilePhysicalSize := FileSeek(FFileHandle, 0 , Ord(soEnd));
Writeln('SizeOfFile: ', aFilePhysicalSize);
FPosition := FileSeek(FFileHandle, aFilePhysicalSize - 17 , Ord(soBeginning));
Writeln('Current position: ', FPosition);
I1 := 17;
ReadFromFile(FFileHandle, I1);

readln;

except

on E: Exception do
begin
Writeln(E.ClassName, ': ', E.Message);
readln;
end;
end;
end.

最佳答案

您正在使用带有未类型化第二个参数的过程 fileread 的版本:

TmpBuffer: TBytes;
...
ReadRes := FileRead(aFileHandle, TmpBuffer, aCount);

但在这种情况下,您应该取消引用动态数组,如 TmpBuffer[0]

来自您的帮助链接:

 //this version is used
function FileRead(Handle: THandle; var Buffer; Count: LongWord): Integer;
//perhaps you wanted that one:
function FileRead(Handle: THandle; var Buffer: TBytes; Offset, Count: LongWord): Integer;

关于windows - 调用 FileRead 后的奇怪计数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48101374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com