如何读写“SET OF”使用 BinaryWriter 输入文件?

发布于 2024-12-03 16:22:46 字数 2458 浏览 1 评论 0原文

这是针对德尔福棱镜的。

假设我有以下枚举 SET 类型,我想将其保存到二进制文件中。

Fruit = (Apple, Banana, Mango, Cherry, Grapes, BlueBerry);
Fruits = set of Fruit;

FruitBasket:Fruits;

with Fruit do
  FruitBasket := [Apple, Mango];

BinaryWriter thefile := new BinaryWriter(File.Create("test.dat"));

thefile.write(FruitBasket);   //<<< raises Error - There is no overloaded method "Write" with these parameters 

thefile.Close;

如何使用 BinaryWriter 将枚举 SET Type(Fruit) 读写到二进制文件中?我以为我在另一个 Stackoverflow 问题中找到了问题的答案,但没有。

我想,我主要必须循环遍历它的元素,但我需要知道是否有更简单的方法来做到这一点。

更新:在第一个答案之后,我尝试并得出了一个快速结论,这对我来说是一个巨大的错误。一旦我解决了程序中的其他问题和错误,我的编译器就会根据 CK 的第一个也是唯一一个答案的建议对我所做的更改提出错误。我只能写,但不能读回来。编译器不断提示 - “类型不匹配,无法将 System.SByte 分配给 Groups.TFeature 集”

顶部的代码只是一个示例。下面是实际的代码:

这是枚举类型:

  TFeature = (tfUnit,tfSignal,tfAlarm,tfControl,tfMaker,tfViewer,tfTrend,
              tfComm,tfSystem,tfScan,tfShutdown,tfPID,tfMagiKal);

这是 SET OF 类型:

  TFeatures = set of TFeature;

这是类:

  TGroup = class
    name:string;
    rwFeatures:TFeatures;
    roFeatures:TFeatures;
    levels:TLevels;
  private

  public
    constructor;
    Method ReadAGrp(bgreader:BinaryReader);
    Method ReadOld(bgreader:BinaryReader);
    Method WriteAGrp(bgwriter:BinaryWriter);
  end;

  TGroupList = class(ArrayList)
  private

  public
    Method ReadGroups(fname:string);
    Method WriteGroups(fname:string);
    Method AddGroup(group1:TGroup);
    Method DeleteGroup(group1:TGroup);
    Method FindGroup(gname:string):TGroup;
  end;

这是我尝试使用 Binarywriter 将 SET OF 类型读取和写入二进制文件的方法:

procedure TGroup.ReadAGrp(bgreader:BinaryReader);
begin
  name:=bgreader.ReadString;
  rwFeatures := TFeature(bgreader.ReadSByte);
  roFeatures := TFeature(bgreader.ReadSByte);
  levels := TLevels(bgreader.readsbyte);
end;

procedure TGroup.ReadOld(bgreader:BinaryReader);
begin
  name:=bgreader.ReadString;
  rwfeatures := TFeature(bgreader.ReadSByte);
  roFeatures := TFeature(bgreader.ReadSByte);
  levels :=TLevels(bgreader.readsbyte);
end;

procedure TGroup.WriteAGrp(bgwriter:BinaryWriter);
begin
  bgwriter.Write(name);
  bgwriter.Write(rwFeatures.toarray);
  bgwriter.Write(roFeatures.ToArray);
  bgWriter.Write(levels.toarray);
end;

如果您可以用示例回答代码或实际代码,我将不胜感激。

如果你没有注意到,我开始为这个问题提供赏金。我真的需要一个可行的答案。谢谢,

谢谢,

This is for Delphi Prism.

Say, I have the following enum SET type that I would like to save into a binary file.

Fruit = (Apple, Banana, Mango, Cherry, Grapes, BlueBerry);
Fruits = set of Fruit;

FruitBasket:Fruits;

with Fruit do
  FruitBasket := [Apple, Mango];

BinaryWriter thefile := new BinaryWriter(File.Create("test.dat"));

thefile.write(FruitBasket);   //<<< raises Error - There is no overloaded method "Write" with these parameters 

thefile.Close;

How do you read and write enum SET Type(Fruit) into a binary file using BinaryWriter? I thought I found the answer to my question in another Stackoverflow question but didn't.

I think, I mostly have to loop through its elements, but I needed to know if there is a simpler way to do it.

UPDATE: After the first answer, I tried and came to a quick conclusion, which was a huge mistake on my part. Once I had other issues and errors in my program sorted out, my compiler raised errors on the changes I made as suggested by the first and the one and only answer by CK. I am only able to write but not read it back. Compiler keeps saying - "Type mismatch, cannot assign System.SByte to set of Groups.TFeature"

Code on the top is just a example. Below is the actual code:

Here is the Enum type:

  TFeature = (tfUnit,tfSignal,tfAlarm,tfControl,tfMaker,tfViewer,tfTrend,
              tfComm,tfSystem,tfScan,tfShutdown,tfPID,tfMagiKal);

Here is the SET OF type:

  TFeatures = set of TFeature;

Here are the classes:

  TGroup = class
    name:string;
    rwFeatures:TFeatures;
    roFeatures:TFeatures;
    levels:TLevels;
  private

  public
    constructor;
    Method ReadAGrp(bgreader:BinaryReader);
    Method ReadOld(bgreader:BinaryReader);
    Method WriteAGrp(bgwriter:BinaryWriter);
  end;

  TGroupList = class(ArrayList)
  private

  public
    Method ReadGroups(fname:string);
    Method WriteGroups(fname:string);
    Method AddGroup(group1:TGroup);
    Method DeleteGroup(group1:TGroup);
    Method FindGroup(gname:string):TGroup;
  end;

Here is how I am trying to read and write SET OF type into a binary file using Binarywriter:

procedure TGroup.ReadAGrp(bgreader:BinaryReader);
begin
  name:=bgreader.ReadString;
  rwFeatures := TFeature(bgreader.ReadSByte);
  roFeatures := TFeature(bgreader.ReadSByte);
  levels := TLevels(bgreader.readsbyte);
end;

procedure TGroup.ReadOld(bgreader:BinaryReader);
begin
  name:=bgreader.ReadString;
  rwfeatures := TFeature(bgreader.ReadSByte);
  roFeatures := TFeature(bgreader.ReadSByte);
  levels :=TLevels(bgreader.readsbyte);
end;

procedure TGroup.WriteAGrp(bgwriter:BinaryWriter);
begin
  bgwriter.Write(name);
  bgwriter.Write(rwFeatures.toarray);
  bgwriter.Write(roFeatures.ToArray);
  bgWriter.Write(levels.toarray);
end;

If you could answer with example code or actual code, I would appreciated.

In case you didn't notice, I started a bounty for this question. I really need a working answer. Thanks,

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轻许诺言 2024-12-10 16:22:46

您可以将其序列化为字节数组;在 FruitBasket 上调用 .ToArray 来获取它,然后使用 new Fruits(myarrayofbyte) 将其作为集合返回。像这样的东西:

  var lData := mySet.ToArray();
  bw.Write(lData.Length); // write the length
  bw.Write(lData); // write the bytes

  // Reading:
  var lData := bw.ReadBytes(bw.ReadInt32());
  var newSet := new Fruits(lData);

you can serialize it as an array of byte; call .ToArray on your FruitBasket to get it, then use new Fruits(myarrayofbyte) to get it back as a set. Something like:

  var lData := mySet.ToArray();
  bw.Write(lData.Length); // write the length
  bw.Write(lData); // write the bytes

  // Reading:
  var lData := bw.ReadBytes(bw.ReadInt32());
  var newSet := new Fruits(lData);
夏末染殇 2024-12-10 16:22:46

基于示例代码(FruitBasket),代码如下。

编写:

thebinarywriter.write(FruitBasket.ToArray);

阅读:

Fruits FruitBasket := new Fruits(thebinaryReader.ReadSByte);

代码已经过测试。

Based on the example code (FruitBasket), the code as follows.

To write:

thebinarywriter.write(FruitBasket.ToArray);

To Read:

Fruits FruitBasket := new Fruits(thebinaryReader.ReadSByte);

The code has been tested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文