Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Problems with TMemoryStream.CopyFrom

Problems with TMemoryStream.CopyFrom

Problems with TMemoryStream.CopyFrom

I have a small problem copying part of a MemoryStream (Stream) into another MemoryStream (FoundStr).
I have a long RTF document that I load into a memory stream and then search for different sections based of a bunch of changing requirements. I then want to load the new MemoryStream into the RichEdit Box (Description_RE).
I have the start position already found :- StartPos: Int64;
I have string from the end of the section I am interested in (s) - happens to be '====================================='
The new stream is "FoundStr"
The source stream "Stream" is currently a global veriable.

The line
FoundStr.CopyFrom(Stream, (EndPos - StartPos));
does not work.
I have the correct position as I have put in the line
in its place and the contents of Buffer1 is correct.

Any ideas what I am doing wrong?

procedure TForm1.GetDescription(StartPos: Int64; S: String);
Buffer1, Buffer2: array[0..254] of Char;
FoundStr : TMemoryStream;
EndPos : Int64;
FillChar(Buffer1, 255, #0);
FillChar(Buffer2, 255, #0);
StrPCopy(@Buffer2, S);
FoundStr := TMemoryStream.Create;
Stream.Position := StartPos;
while (not CompareMem(@Buffer1,@Buffer2,Length(S))) do
if Stream.Position = Stream.Size then exit;
Stream.Position := Stream.Position - (Length(S) - 1);
Stream.Read(Buffer1[0], Length(S));
EndPos := Stream.Position - Length(S);
Stream.Position := StartPos;
FoundStr.CopyFrom(Stream, (EndPos - StartPos));



RE: Problems with TMemoryStream.CopyFrom

Should have pointed out that there is no error.
Simply nothing shows up in the RichEdit box

RE: Problems with TMemoryStream.CopyFrom

OK, Basic mistake. Forgot to set FoundStr.Position := 0; before loading it into the RE.
New Problem though:-
No formatting copies over.
There is text and tables in the selection area and all the formatting is lost.
Is there a way around this?

RE: Problems with TMemoryStream.CopyFrom

Why the memorystreams? Seems much more simple to work with TStringStream or ordinary strings.
As for the RTF, what is the value of the PlainText property of the TRichEdit?


Helping people is my job...

RE: Problems with TMemoryStream.CopyFrom

I would be happy to use TStringStream or ordinary strings if they retained the formatting.
PlainText property is False.
I am trying to do the same process now by loading the file into Temp_RE, Searching and selecting in there and copying the selected area to Description_RE.
Still Struggling with it at the moment. The search code I have found takes quite a while.
I am not sure if this is the best method.

procedure TForm1.GetDescription(S1, S2: String);
StartPos, Position1, Position2, Endpos: Integer;

StartPos := 0;
Endpos := Length(Temp_RE.Text);
while Temp_RE.FindText(S1, StartPos, Endpos, [stMatchCase])<>-1 do
Endpos := Length(Temp_RE.Text) - StartPos;
Position1 := Temp_RE.FindText(S1, StartPos, Endpos, [stMatchCase]);
Inc(StartPos, Length(S1));
Temp_RE.SelStart := Position1;
Temp_RE.SelLength := Length(S1);

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close