Saving TRichEdit to a binary file.

Hi,
How can I save TRichEdit containing characters with different colors to a binary file ?
My problem is not in saving TStringList to a text file using SaveToFile and LoadFromFile but to save TStringList as a part of a record to file of record.
MotiNAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MotiNAuthor Commented:
Adjusted points to 150
0
edeyCommented:
is the TRichEdit.lines.saveToFile method not suffecient?


GL
Mike
0
PeterLarsenCommented:
Exactly what i would ask. Why do you want to save the text in a binary file ??

Try this. This save it all...

FontDialog1.Font.Assign(RichEdit1.SelAttributes);
if FontDialog1.Execute then
 if RichEdit1.SelLength > 0 then
   RichEdit1.SelAttributes.Assign(FontDialog1.Font)
 else
   RichEdit1.DefAttributes.Assign(FontDialog1.Font);
RichEdit1.lines.SaveToFile('e:\temp\RichEdit');
RichEdit2.lines.loadfromFile('e:\temp\RichEdit');
RichEdit2.SetFocus;

Peter
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

kretzschmarCommented:
? what does you mean with binary file ?
0
MotiNAuthor Commented:
I defined a record -

   TRecord = Record
      aContent : TStringList;
      .
      .
      .
   end; {TRecord}

and I want to define a file of TRecord
( fileOfTRecord : file of TRecord; )  so I can save and load all TRecord fields.
0
MotiNAuthor Commented:
Edited text of question.
0
MotiNAuthor Commented:
Sorry for confusing you, please read the question again.
0
philipleighsCommented:
How about using Lines.SaveToStream. Create and write to a memory stream, and there's your chunk of binary data!

0
edeyCommented:
This is more difficult as a TStringList (obviously) has no fixed size/length.  Hence you can not declare a "File of TRecord" as compiler wouldn't know what size to use for the record (actually it would, but the field for the TStringList would merely be a cardinal with the value of integer(pointer(aContent)), not very usefull)  I would then recomend that you (in the file) define a header that is of fixed length and contains the rest of the records contents, as well as the length(aContent.text).  This way you could:

1)Read in a header worth of data
2)get the TStringList's size
3)read in length(aContent) worth of data
4)repeat 1-3, as nec.


GL
Mike

ps. Compound docs may also be considered, esp. if the TStringList is not the only dynamicaly sized field of TRecord.
0
MotiNAuthor Commented:
Can you be more specific phil ?
0
MotiNAuthor Commented:
edey,
TStringList is not the only dynamically sized fields in TRecord. What do you mean in 'Compound docs' ?
In your solution you define a fixed size for TStringList and I try to avoid this. I want to save its dynamically.
0
philipleighsCommented:
OK, well I'm interpreting your situation as requiring many TRichEdits to be stored in a single file (like a database of rich text). If so then I would download a component that has just been uploaded to the DSP called HKStreams.zip (or something) which is a component that lets you write many streams to a single file. Each stream is identified with a name.

To save a number of richedits to the same file, do something like this:
var
  MultiStream: THKStreams;
  ms: TMemoryStream;
begin
  MultiStream := THKStreams.Create;
  ms := TMemoryStream.Create;
  try
    MultiStream.Compressed := true;
    RichEdit1.Lines.SaveToStream(ms);
    MultiStream.AddStream('KeyName', ms);
    //You can call AddStream many times.
    MultiStream.SaveToFile('Bob.dat');    
  finally
    ms.Free;
    MultiStream.Free;
  end;
end;


Read it back in like this:
var
  MultiStream: THKStreams;
  ms: TMemoryStream;
begin
  MultiStream := THKStreams.Create;
  ms := TMemoryStream.Create;
  try
    MultiStream.LoadFromFile('Bob.dat');
    MultiStream.GetStream('KeyName', ms);
    RichEdit1.Lines.LoadFromStream(ms);
  finally
    ms.Free;
    MultiStream.Free;
  end;
end;

Cheers,
Phil.

PS. DSP = http://sunsite.icm.edu.pl/delphi/
Click on New files or search for "hkstr" or I can mail the component.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
edeyCommented:
>TStringList is not the only
>dynamically sized fields in TRecord.

perhaps some more info about the record definition would help as this would seem a little more complicated then "Saving TRichEdit to a binary file." :)

>What do you mean in 'Compound docs' ?

An OLE Compound Document (or whatever M$ is calling them these days) if essentially a file that contains a file system, kinda like a 'folder' within a file.  Using these you can stuff many files into a single file.

>In your solution you define a fixed
>size for TStringList and I try to avoid
>this. I want to save its dynamically.

actually, what I tried to show was a method of saving a TStringList whose size is known at 'save time', but not 'compile time'. so this may be clearer:

when writing:
1)Fill a header with the rest of the records data
2)get the TStringList's size
3)save in the header the value length(aContent)data
4)append aContent.text to the file
5)repeat 1-4 as nec.

when reading:
1)Read in a header worth of data
2)get the TStringList's size
3)read in length(aContent) worth of data
4)repeat 1-3, as nec.



GL
Mike
0
MotiNAuthor Commented:
Thanks you all...  :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.