gpt4 book ai didi

delphi - 为什么 TFileStream.write 产生不正确的数据?德尔福7

转载 作者:行者123 更新时间:2023-12-02 12:52:13 24 4
gpt4 key购买 nike

我试图创建一个函数/方法,它应该写入一个文件一个用零填充的标题

header := StringOfChar(char(0), 11*offsetSize);

但是当我检查创建的文件时,它具有以下值(十六进制):

C026A200160000000100000006000000264C656B63650000B2000000B04D45005C1EA200306CA20000000000

我预计输出文件应包含

0000 0000 0000 0000 0000 0000

然后我也尝试用 chr(1) 填充它,结果类似:

C026A200160000000100000006000000264C656B63650000B2000000B04D45005C1EA200306CA20000000000

(看起来一样)。

当我在 Delphi 7 中调试时,我会观察标题是否填充了零:

#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0

当我用 chr(1) 填充它时,它包含...

#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1#1

所以我的问题是,为什么会发生这种情况?我做错了什么,输出文件包含错误的数据?

unit Dictionary_io;

interface

uses
Classes, Windows, SysUtils, Dialogs;

const
offsetTableEntriesCount = 10;

type
TCountType = dword;
TOffsetType = dword;
TTextType = ANSIString;

const
testItemsCount = 1;

type
Dictionary = class
private
fsInput, fsOutput: TFileStream;
fileName: string;
msOffsets: TMemoryStream;
offsetSize: TOffsetType;
ofsTableEntries: TCountType;
lng: integer;
itemsCount: byte;
header: TTextType;
public
constructor Create;
procedure dicRewrite;
end;

implementation

constructor Dictionary.Create;
begin
offsetSize := sizeof(offsetSize);
end;

procedure Dictionary.dicRewrite;
var
i: integer;
begin
itemsCount := 0;
header := StringOfChar(char(0), 11*offsetSize);
try
fileName := 'A:\test.bin';
if FileExists(fileName) then
DeleteFile(fileName);
fsOutput := TFileStream.Create(fileName, fmCreate);
try
fsOutput.Write(header,Length(header));
finally
end;
finally
fsOutput.Free;
end;
end;

end.

最佳答案

您的数据未正确写入文件的原因是您未正确将 header 传递给 Write()Write() 接受无类型的 var 作为输入,并且您按原样向其传递 header 变量,因此您传递 Write() header 本身的内存地址。但是字符串包含一个指向存储在内存中其他位置的字符数据的指针。因此,为了将该数据写入文件,您需要取消引用 string 指针,以便传入字符数据的内存地址。

此外,如果您不想检查 Write() 的返回值是否有错误,则应使用 WriteBuffer() 代替。

试试这个:

procedure Dictionary.dicRewrite;
var
i: integer;
begin
itemsCount := 0;
header := StringOfChar(#0, 11*offsetSize);
fileName := 'A:\test.bin';
fsOutput := TFileStream.Create(fileName, fmCreate);
try
fsOutput.WriteBuffer(header[1], Length(header));
finally
fsOutput.Free;
end;
end;

或者:

procedure Dictionary.dicRewrite;
var
i: integer;
begin
itemsCount := 0;
header := StringOfChar(#0, 11*offsetSize);
fileName := 'A:\test.bin';
fsOutput := TFileStream.Create(fileName, fmCreate);
try
fsOutput.WriteBuffer(PAnsiChar(header)^, Length(header));
finally
fsOutput.Free;
end;
end;

话虽这么说,您确实不应该对二进制数据使用字符串(特别是如果您计划将项目升级到Delphi 2009+)。您应该使用记录 code> 或字节数组,例如:

private
header: array of Byte;

procedure Dictionary.dicRewrite;
var
i: integer;
begin
itemsCount := 0;
SetLength(header, 11*offsetSize);
FillChar(header[0], Length(header), #0);
fileName := 'A:\test.bin';
fsOutput := TFileStream.Create(fileName, fmCreate);
try
fsOutput.WriteBuffer(header[0], Length(header));
finally
fsOutput.Free;
end;
end;

或者:

procedure Dictionary.dicRewrite;
var
i: integer;
begin
itemsCount := 0;
SetLength(header, 11*offsetSize);
FillChar(PByte(header)^, Length(header), #0);
fileName := 'A:\test.bin';
fsOutput := TFileStream.Create(fileName, fmCreate);
try
fsOutput.WriteBuffer(PByte(header)^, Length(header));
finally
fsOutput.Free;
end;
end;

或者:

private
header: array[0..(11*offsetSize)-1] of Byte;

procedure Dictionary.dicRewrite;
var
i: integer;
begin
itemsCount := 0;
FillChar(header, Length(header), #0);
fileName := 'A:\test.bin';
fsOutput := TFileStream.Create(fileName, fmCreate);
try
fsOutput.WriteBuffer(header, SizeOf(header));
finally
fsOutput.Free;
end;
end;

关于delphi - 为什么 TFileStream.write 产生不正确的数据?德尔福7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51226714/

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