How to read and write “SET OF” type into a file using BinaryWriter?
-
28-10-2019 - |
Question
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,
Solution
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);
OTHER TIPS
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.