Board index » delphi » multiple RichEdit text to single file

multiple RichEdit text to single file

Hi
I have a number of RichEdit boxes, containing some formatted text.  I would
like the user to be able to click a "save" button, and have all the
formatted text in each richedit to be saved in a single file; then if the
user wants to load that file back into the app, each richedit will have the
formatted text it contained when the file was saved.

I thought of making the user select a folder, then creating an individual
RTF file for each richedit, then when the user wants to load the info back
in, they select the source folder... However I think 7 or 8 RTF files will
messy and harder for the user to backup.

Can anyone please suggest a solution to my problem; ie.  a way to create a
single file where multiple richedit formatted texts can be saved, and then
loaded back into their respective richedits at a later time.

Thanks.
-Adam.

---
Incoming mail is certified Virus Free by Grisoft AVG.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.449 / Virus Database: 251 - Release Date: 27/01/2003

 

Re:multiple RichEdit text to single file


In article <3e479e2...@mercury.planet.net.au>, "Adam M"

Quote
<REMOVE-THISad...@froggy.com.au> writes:
>I have a number of RichEdit boxes, containing some formatted text.  I would
>like the user to be able to click a "save" button, and have all the
>formatted text in each richedit to be saved in a single file; then if the
>user wants to load that file back into the app, each richedit will have the
>formatted text it contained when the file was saved.

>I thought of making the user select a folder, then creating an individual
>RTF file for each richedit, then when the user wants to load the info back
>in, they select the source folder... However I think 7 or 8 RTF files will
>messy and harder for the user to backup.

>Can anyone please suggest a solution to my problem; ie.  a way to create a
>single file where multiple richedit formatted texts can be saved, and then
>loaded back into their respective richedits at a later time.

In general, use a memory stream and TRichEdit.Lines.SaveToStream (after setting
TRichEdit.PlainText to false) maybe naming it RichEditMS.

In particular use the above plus a TFileStream. Write the length of the above
stream to the filestream, then use TfileStream.CopyFrom(RichEditMS) to get the
first richedit into the stream.

Repeat for all other richedits.

Note that you will have to position the streams before issuing a CopyFrom.

If you are not confident with streams there is a copy of a help file I wrote on
the MiniFaq for comp.lang.pascal.delphi.misc.

You're only going to need about 5 lines of code plus 5 lines / rich edit to do
the whole thing.

Loading from file is much the same ...

Read the number of bytes, load that number of bytes into a memory stream then
LoadFromStream into the richedit, and repeat for all richedits.

Alan Lloyd
alangll...@aol.com

Re:multiple RichEdit text to single file


It's no problem to write all the text in one file.
Just write the length of each text as an integer before
the text and when reading you can assign the length
of the text fields again and read back the text.
 (using BlockWrite or TFileStream)
that way the file will contain:

{sz1(integer)}{richedit1.text,(length sz1)}
{sz2(integer)}{richedit2.text,(length sz2)}
...
when reading the text back you can read the
length first, set the length of the text, read the
text, read the next length a.s.o.

"Adam M" <REMOVE-THISad...@froggy.com.au> schrieb im Newsbeitrag
news:3e479e20.0@mercury.planet.net.au...

Quote
> Hi
> I have a number of RichEdit boxes, containing some formatted text.  I
would
> like the user to be able to click a "save" button, and have all the
> formatted text in each richedit to be saved in a single file; then if the
> user wants to load that file back into the app, each richedit will have
the
> formatted text it contained when the file was saved.

> I thought of making the user select a folder, then creating an individual
> RTF file for each richedit, then when the user wants to load the info back
> in, they select the source folder... However I think 7 or 8 RTF files will
> messy and harder for the user to backup.

> Can anyone please suggest a solution to my problem; ie.  a way to create a
> single file where multiple richedit formatted texts can be saved, and then
> loaded back into their respective richedits at a later time.

> Thanks.
> -Adam.

> ---
> Incoming mail is certified Virus Free by Grisoft AVG.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.449 / Virus Database: 251 - Release Date: 27/01/2003

Re:multiple RichEdit text to single file


Quote
Adam M wrote:
> Can anyone please suggest a solution to my problem; ie.  a way to
> create a single file where multiple richedit formatted texts can be
> saved, and then loaded back into their respective richedits at a
> later time.

Sorry for the huge paste, but all those try...finally's are necessary to
make the compiler happy. You can leave them out if you want. OK, here goes:

// save the content of multiple richedit's to a file

procedure TForm1.SaveClick(Sender: TObject);
var
   FileStream: TFileStream;
   Writer: TWriter;
   StringStream: TStringStream;
begin
    FileStream := TFileStream.Create('text.dat', fmCreate);
    try
        Writer := TWriter.Create(FileStream, 4096);
        try
            StringStream := TStringStream.Create('');
            try
                RichEdit1.Lines.SaveToStream(StringStream);
                Writer.WriteString(StringStream.DataString);
            finally
                StringStream.Free
            end;

            StringStream := TStringStream.Create('');
            try
                RichEdit2.Lines.SaveToStream(StringStream);
                Writer.WriteString(StringStream.DataString);
            finally
                StringStream.Free;
            end;
        finally
            Writer.Free;
        end;
    finally
        FileStream.Free;
    end;
end;

// and it's counterpart, loading the file into multiple richedit's

procedure TForm1.LoadClick(Sender: TObject);
var
   FileStream: TFileStream;
   Reader: TReader;
   StringStream: TStringStream;
begin
    FileStream := TFileStream.Create('text.dat', fmOpenRead);
    try
        Reader := TReader.Create(FileStream, 4096);
        try
            StringStream := TStringStream.Create(Reader.ReadString);
            try
                RichEdit1.Lines.LoadFromStream(StringStream);
            finally
                StringStream.Free;
            end;

            StringStream := TStringStream.Create(Reader.ReadString);
            try
                RichEdit2.Lines.LoadFromStream(StringStream);
            finally
                StringStream.Free;
            end;
        finally
            Reader.Free;
        end;
    finally
        FileStream.Free;
    end;
end;

This is a much cleaner (ahum!) way of saving to a file. The advantage of
this method is that you don't have to fiddle with saving the length of the
text etc. Well, in fact the length is saved to the file through the TWriter
object. But you don't see it (and it's almost always a good thing(tm) to
keep things transparent). There is some optimalization (size! not speed)
possible by saving the contents of the richedit's in a loop though.

--
www.zenobits.com

Re:multiple RichEdit text to single file


Quote
"Adam M" <REMOVE-THISad...@froggy.com.au> wrote in message

news:3e479e20.0@mercury.planet.net.au...

Quote
> Hi
> I have a number of RichEdit boxes, containing some formatted text.  I
would
> like the user to be able to click a "save" button, and have all the
> formatted text in each richedit to be saved in a single file; then if the
> user wants to load that file back into the app, each richedit will have
the
> formatted text it contained when the file was saved.

> I thought of making the user select a folder, then creating an individual
> RTF file for each richedit, then when the user wants to load the info back
> in, they select the source folder... However I think 7 or 8 RTF files will
> messy and harder for the user to backup.

> Can anyone please suggest a solution to my problem; ie.  a way to create a
> single file where multiple richedit formatted texts can be saved, and then
> loaded back into their respective richedits at a later time.

> Thanks.
> -Adam.

> ---
> Incoming mail is certified Virus Free by Grisoft AVG.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.449 / Virus Database: 251 - Release Date: 27/01/2003

Thanks to everyone who replied :-)

---
Incoming mail is certified Virus Free by Grisoft AVG.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.449 / Virus Database: 251 - Release Date: 27/01/2003

Re:multiple RichEdit text to single file


In article <20030210090628.25546.00000...@mb-me.aol.com>, alangll...@aol.com

Quote
(AlanGLLoyd) writes:
>In general, use a memory stream and TRichEdit.Lines.SaveToStream (after
>setting
>TRichEdit.PlainText to false) maybe naming it RichEditMS.

>In particular use the above plus a TFileStream. Write the length of the above
>stream to the filestream, then use TfileStream.CopyFrom(RichEditMS) to get
>the
>first richedit into the stream.

>Repeat for all other richedits.

It looks easier than I thought (but untested), and these do any number of
richedits...

uses
  ComCtrls;

procedure SaveRichEditsToFile(REArray : array of TRichEdit; FPN : string);
{the richedits should have their PlainText set as desired
 before calling this proc}
var
  FS : TFileStream;
  i, MSSize : integer;
  MS : TMemoryStream;
  REPlainText : boolean;
begin
  FS := TFileStream.Create(FPN, fmCreate);
  FS.Write(High(REArray), SizeOf(integer));
  for i := 0 to High(REArray) do begin
    MS := TMemoryStream.Create;
    REPlainText := REArray[i].PlainText;
    FS.Write(REPlainText, SizeOf(boolean));
    REArray[i].Lines.SaveToStream(MS);
    MSSize := MS.Size;
    FS.Write(MSSize, SizeOf(integer));
    FS.CopyFrom(MS, 0);
    MS.Free;
  end;
  FS.Free;
end;

procedure LoadRichEditsFromFile(REArray : array of TRichEdit; FPN : string);
{the richedits will have their PlainText set as when saved to file,
 after this proc returns}
var
  FS : TFileStream;
  i, MSSize, HighREArray : integer;
  MS : TMemoryStream;
  REPlainText : boolean;
begin
  FS := TFileStream.Create(FPN, fmOpenRead);
  FS.Read(HighREArray, SizeOf(integer));
  if (High(REArray) <> HighREArray) then begin
    FS.Free;
    Exit;
  end;
  for i := 0 to High(REArray) do begin
    MS := TMemoryStream.Create;
    FS.Read(REPlainText, SizeOf(boolean));
    REArray[i].PlainText := REPlainText;
    FS.Read(MSSize, SizeOf(integer));
    MS.CopyFrom(FS, MSSize);
    MS.Seek(0, soFromBeginning);
    REArray[i].Lines.LoadFromStream(MS);
    MS.Free;
  end;
  FS.Free;
end;

Alan Lloyd
alangll...@aol.com

Other Threads