Solved

Save & Load Sets?

Posted on 2004-03-29
6
200 Views
Last Modified: 2010-04-05
I am trying to save & load a large amount of set data, to and from file using streams.

I have made an example of what I am trying to do.

It seams the writing to file works fine, but it crashes on the loading from file portion

Here is basically what I have:

Type

 TAnimal = (Dog, Cat, Horse, Cow, Squirrel);
 TAnimals = set of TAnimal;


var
 Animals: TAnimals;
 Animal: TAnimal;


//returns the number of items in Set
function GetAnimalCount(AAnimals: TAnimals): Integer;
var
 AAnimal: TAnimal;
begin
  result:= 0;
  for AAnimal:=  Low(AAnimal) to High(AANimal) do
   if AAnimal in AAnimals then  Result:= Result + 1;
end;



Here is where I save to file

 MemStr:= TMemoryStream.Create;

 WriteStreamInt(MemStr, GetAnimalCount(Animals));
 
 for Animal := Low(Animal) to High(Animal) do
  if Animal in Animals then
   WriteStreamStr(MemStr, GetEnumName(TypeInfo(TAnimal), Ord(Animal)));

  MemStr.SaveToFile(fFileName);
  MemStr.Free


here is where I load from file


 MemStr:= TMemoryStream.Create;
 MemStr.LoadFromFile(fFileName);
   
Count:= ReadStreamInt(MemStr);
for I:= 0 to Count - 1 do
 begin
  AnimalStr:= ReadStreamStr(MemStr);
  Include(Animals, TAnimal(GetEnumValue(TypeInfo(TAnimal),AnimalStr)));
 end;
MemStr.Free;

Using a showmessage in teh load from file, i can show the first item read in, but then it crashes.

Shane



0
Comment
Question by:shaneholmes
  • 4
  • 2
6 Comments
 
LVL 11

Author Comment

by:shaneholmes
ID: 10709975
Sorry, typo mistake


for Animal := Low(TAnimal) to High(TAnimal) do

Shane
0
 
LVL 11

Author Comment

by:shaneholmes
ID: 10710039
This is definitly where it crashes  

Include(Animals, TAnimal(GetEnumValue(TypeInfo(TAnimal),AnimalStr)));
 
I have also tried:

Animals:= Animals + TAnimal(GetEnumValue(TypeInfo(TAnimal),AnimalStr));

and

Animals:= Animals + [TAnimal(GetEnumValue(TypeInfo(TAnimal),AnimalStr))];

Shane
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 250 total points
ID: 10710157

Not sure if this helps, but the following compiles and executes correctly on D5.

// Read/Write functions taken from TReader and TWriter respectively

function GetAnimalCount(AAnimals: TAnimals): Integer;
var  AAnimal:    TAnimal;
begin
  result:=0;
  for AAnimal:=Low(AAnimal) to High(AAnimal) do
     if AAnimal in AAnimals then  Inc(result);
end;

procedure WriteStreamInt(Stream: TStream; Value: Integer);
begin
  Stream.Write(Value, SizeOf(Value));
end;

function ReadStreamInt(Stream: TStream): Integer;
begin
  Stream.Read(result, SizeOf(Integer));
end;

function ReadStreamStr(Stream: TStream): String;
var  dwLength:   Integer;
begin
  Stream.Read(dwLength, SizeOf(Integer));
  SetLength(result, dwLength);
  Stream.Read(Pointer(result)^, dwLength);
end;

procedure WriteStreamStr(Stream: TStream; Value: String);
var  dwLength:   Integer;
begin
  dwLength:=Length(Value);
  Stream.Write(dwLength, SizeOf(Integer));
  Stream.Write(Pointer(Value)^, dwLength);
end;

procedure SetSaveToFile(FileName: String);
var  MemStream:  TMemoryStream;
     AAnimal:    TAnimal;
begin
  MemStream:=TMemoryStream.Create;
  WriteStreamInt(MemStream, GetAnimalCount(Animals));
  for AAnimal:=Low(Animal) to High(Animal) do
     if AAnimal in Animals then
        WriteStreamStr(MemStream, GetEnumName(TypeInfo(TAnimal), Ord(AAnimal)));
  MemStream.SaveToFile(FileName);
  MemStream.Free
end;

procedure SetLoadFromFile(FileName: String);
var  MemStream:  TMemoryStream;
     dwIndex:    Integer;
     dwCount:    Integer;
begin
  MemStream:=TMemoryStream.Create;
  MemStream.LoadFromFile(FileName);
  Animals:=[];
  dwCount:=ReadStreamInt(MemStream);
  for dwIndex:=0 to Pred(dwCount) do
     Include(Animals, TAnimal(GetEnumValue(TypeInfo(TAnimal), ReadStreamStr(MemStream))));
  MemStream.Free
end;

----

begin

  Animals:=[Dog, Horse];
  SetSaveToFile('c:\test.txt');
  Animals:=[];
  SetLoadFromFile('c:\test.txt');
  if Animals = [Dog, Horse] then
     ShowMessage('Correct');

end;


Regards,
Russell

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 11

Author Comment

by:shaneholmes
ID: 10710174
Russel, im in the middle of something right now, but i will get back to this in a bit and test it.

If it compiles and runs on your machine, im sure it will on mine.

Thanks!

Shane

0
 
LVL 11

Author Comment

by:shaneholmes
ID: 10710251
Hey Russell, after reviewing your code, I found two problems, fixed them and boom - works like a charm.

Sometimes youc an stare at code for ever and never see it. I knew smeone else would be able to right away!

Thanks for the boost!

Shane
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10710268
Anytime ;-)

And thank you,
Russell

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now