gpt4 book ai didi

delphi - Delphi 2009下将字符串保存到磁盘

转载 作者:行者123 更新时间:2023-12-02 03:08:20 25 4
gpt4 key购买 nike

我有一个如下所示的结构,需要保存并从磁盘加载。

 RSecStructure= packed record
Name : string[255]; {NEED UNICODE SUPPORT HERE}
ScreenName : string[255];
OrigFileName : string[255];
Prim : string[255];
ParentVersion : integer;
sTag1 : string[255];
sTag2 : string[255];
sTag3 : string[255];
sTag4 : string[255];
DateAdd : TDateTime;
DateModify : TDateTime;
end;

到目前为止,我已经使用类似的方法来保存结构:

function
var F: FILE;
Hdr: RSecStructure;
begin
...
BlockWrite (F, Hdr, SizeOf(Hdr));
...
end

上面的代码在 Delphi 7 下工作。在 D2009 下,当我在短字符串和 Unicode 字符串之间进行分配时,我收到了很多警告消息。到目前为止,我设法在没有任何编译器警告或提示的情况下编写 Delphi 代码,并且我希望保持这种状态。所以我需要一个优雅的方法来将字符串(Unicode 很好,但并不重要)保存到磁盘而不收到警告。

最佳答案

这些字符串字段在 Delphi 2009 中与所有以前的版本中相同。 ShortString 不是 Unicode 类型。

因此您应该能够继续按原样使用该记录。

你说它在 Delphi 7 中工作。它在 Delphi 2009 中工作吗?描述您遇到的问题。

您的意思是说您想要一个相当于 ShortString 的固定长度 Unicode?没有这样的记录,因此您不能拥有这样的记录,让它保存类似字符串的 Unicode 值,并将其直接保存到磁盘。

不过,我认为这不是一个主要问题,因为您的磁盘格式无论如何都不会与您当前的格式兼容:您的字符太大。

您可以使用字符数组:

type
TSecStructure = packed record
Name : array[0..255] of UnicodeChar;
ScreenName : array[0..255] of UnicodeChar;
OrigFileName : array[0..255] of UnicodeChar;
Prim : array[0..255] of UnicodeChar;
ParentVersion : integer;
sTag1 : array[0..255] of UnicodeChar;
sTag2 : array[0..255] of UnicodeChar;
sTag3 : array[0..255] of UnicodeChar;
sTag4 : array[0..255] of UnicodeChar;
DateAdd : TDateTime;
DateModify : TDateTime;
end;

这不会像真正的字符串类型那么方便,但它适用于大多数用途。

您还可以使用普通的 UnicodeString 类型:

type
TSecStructure = record
Name : UnicodeString;
ScreenName : UnicodeString;
OrigFileName : UnicodeString;
Prim : UnicodeString;
ParentVersion : integer;
sTag1 : UnicodeString;
sTag2 : UnicodeString;
sTag3 : UnicodeString;
sTag4 : UnicodeString;
DateAdd : TDateTime;
DateModify : TDateTime;
end;

您无法再将其直接保存到磁盘,但也不再限制为 255 个字符。您必须单独存储每个字符串字段。确保也存储字符串的长度,否则当稍后加载文件时,您将不知道一个字符串在哪里结束,下一个字符串从哪里开始。

关于delphi - Delphi 2009下将字符串保存到磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/622761/

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