.net “如何读写”;一套;使用BinaryWriter键入文件?
这是给德尔福棱镜的 比方说,我有下面的枚举集类型,我想保存到一个二进制文件中.net “如何读写”;一套;使用BinaryWriter键入文件?,.net,delphi,set,delphi-prism,binarywriter,.net,Delphi,Set,Delphi Prism,Binarywriter,这是给德尔福棱镜的 比方说,我有下面的枚举集类型,我想保存到一个二进制文件中 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.
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;
以下是一组类型:
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将类型集读写到二进制文件中:
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;
如果您能用示例代码或实际代码回答,我将不胜感激
如果你没注意到,我开始悬赏这个问题。我真的需要一个有效的答案。谢谢
谢谢,您可以将其序列化为字节数组;打电话到你的果篮里去拿,然后用新的水果(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); var lData:=mySet.ToArray(); bw.Write(lData.Length);//写下长度 bw.Write(lData);//写入字节 //阅读: var lData:=bw.ReadBytes(bw.ReadInt32()); var newSet:=新水果(lData); 基于示例代码(果篮),代码如下所示 写:
thebinarywriter.write(FruitBasket.ToArray);
全文如下:
Fruits FruitBasket := new Fruits(thebinaryReader.ReadSByte);
代码已经过测试。@Ck,谢谢您的回答。我会写,但不会读回。它说它不能从字节转换为设置数据类型(水果)。@CK,我以为它能工作,但我过早下结论。在处理完其他错误后,我终于让我的程序进行编译,现在当我从文件中读取它时,它会给我错误。我可以使用ToArray写入文件,但当我使用readSbyte方法时,它失败了。虽然它最初是编译的,但它还有其他错误。所以,我想,直到现在,它还没有解决这个错误。这就是为什么我还没有接受你的答案。
Fruits FruitBasket := new Fruits(thebinaryReader.ReadSByte);