我正在尝试衡量FILE_FLAG_WRITE_THROUGH
和FILE_FLAG_NO_BUFFERING
对文件中写入序列的影响,作为another question中的请求。但我发现我无法用FILE_FLAG_NO_BUFFERING
设置文件。
当我使用它时,Delphi会返回EWriteError
消息stream read error
。
使用的代码如下:
procedure TForm1.btn1Click(Sender: TObject);
var
fsFSArquivoAAC: TFileStream;
L, lastErr: Cardinal;
R: WideString;
hn: THandle;
begin
hn := Windows.CreateFile( PChar('TesteAAC.AAC2'),
GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE, nil, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL or FILE_FLAG_WRITE_THROUGH or FILE_FLAG_NO_BUFFERING, 0);
lastErr := GetLastError();
if (lastErr <> ERROR_SUCCESS) then
begin
if (lastErr <> ERROR_ALREADY_EXISTS ) then
begin
MessageDlg('Whoops, something went wrong with CreateFile!',
mtError, [mbOK], 0);
end
else
begin
SetLastError(ERROR_SUCCESS);
end;
end;
fsFSArquivoAAC := TFileStream.Create( hn );
try
R := 'BatatinhaquandoNasceEspalharamapelochao';
// write WideString
L := Length(R);
fsFSArquivoAAC.WriteBuffer(L, SizeOf(integer));
if L > 0 then
fsFSArquivoAAC.WriteBuffer(R[1], L * SizeOf(WideChar));
finally
fsFSArquivoAAC.Free;
end;
如果您评论FILE_FLAG_NO_BUFFERING
代码有效。为什么呢?
答案 0 :(得分:7)
如果使用FILE_FLAG_WRITE_THROUGH
和FILE_FLAG_NO_BUFFERING
,则various requirements用于在内存中对齐缓冲区,将写入与磁盘扇区对齐,并且(我认为)以扇区大小的倍数进行写入。你似乎没有做这些事情。