Board index » delphi » Streaming content of a memo to a file

Streaming content of a memo to a file

Hi,

I try to stream the contents of a memo Memo1 to a file c:\test.txt:

procedure TForm1.Button1Click(Sender: TObject);
var
f : TFileStream;
  s : string;
begin
  f.Create('c:\test.txt', fmCreate or fmShareDenyNone);
  s := Memo1.Lines.Text;
  f.WriteBuffer(s, SizeOf(s));
  f.Destroy;
end;

Something seems wrong with this, since I get an error message. Can anyone
help me ?

Thanks already,
Peter

 

Re:Streaming content of a memo to a file


Quote
Peter Bos wrote:
>   f.Create('c:\test.txt', fmCreate or fmShareDenyNone);

f:=TFileStream.Create('c:\test.txt',fmCreate or fmShareDenyNone);

Alex
--
** Pepsi Cola and animal protection **
**  http://www.pepsi{*word*76}bath.com/  **

Author of the free Chatsystem PINO! || Website: http://www.alcomp.net
Available at http://pino.cjb.net    || Chat   : pinochat.dhs.org:8080

Re:Streaming content of a memo to a file


procedure tForm1.Button1Click (Sender : tObject);

begin
Memo1.Lines.SaveToFile ('c:\test.txt');
end;

Quote
"Peter Bos" <peter....@cable.a2000.nl> wrote in message

news:855gpi$s20$1@weber.a2000.nl...
Quote
> Hi,

> I try to stream the contents of a memo Memo1 to a file c:\test.txt:

> procedure TForm1.Button1Click(Sender: TObject);
> var
> f : TFileStream;
>   s : string;
> begin
>   f.Create('c:\test.txt', fmCreate or fmShareDenyNone);
>   s := Memo1.Lines.Text;
>   f.WriteBuffer(s, SizeOf(s));
>   f.Destroy;
> end;

> Something seems wrong with this, since I get an error message. Can anyone
> help me ?

> Thanks already,
> Peter

Re:Streaming content of a memo to a file


In article <855gpi$s2...@weber.a2000.nl>, "Peter Bos"

Quote
<peter....@cable.a2000.nl> writes:
>  s := Memo1.Lines.Text;
>  f.WriteBuffer(s, SizeOf(s));

... and

  F.WriteBuffer(S[1], Length(S));

S is a string and should be handled this way.

Alan Lloyd
alangll...@aol.com

Re:Streaming content of a memo to a file


Quote
AlanGLLoyd <alangll...@aol.com> wrote in message

news:20000108023338.12489.00000431@nso-fa.aol.com...

Quote
> In article <855gpi$s2...@weber.a2000.nl>, "Peter Bos"
> <peter....@cable.a2000.nl> writes:

>   F.WriteBuffer(S[1], Length(S));

> S is a string and should be handled this way.

> Alan Lloyd
> alangll...@aol.com

This works indeed, but how do I read the buffer ? I tried it like this, but
it doesn't work (f.Position does not change after calling f.ReadBuffer):

Writing:

var
  w : word;
  s : string;
begin
  ...
  s := Memo1.Lines.Text;
  w := Length(s);
  f.Write(w, SizeOf(w));
  f.WriteBuffer(s[1], w);
end;

Reading:

var
  w : word;
  s : string;
begin
  ...
  try
    f.Read(w, SizeOf(w));
    f.ReadBuffer(s[1], w);
    Memo1.Lines.Text := s;
  except
    {...}
  end;
end;

Can someone help me again ?

Thanks,
Peter

Re:Streaming content of a memo to a file


In article <85dk3a$n...@tesla.a2000.nl>, "Peter Bos" <peter....@cable.a2000.nl>
writes:

Quote
>var
>  w : word;
>  s : string;
>begin
>  ...
>  try
>    f.Read(w, SizeOf(w));
>    f.ReadBuffer(s[1], w);
>    Memo1.Lines.Text := s;
>  except
>    {...}
>  end;

Should be :-

var
  w : word;
  s : string;
begin
  ...
  try
    f.Read(w, SizeOf(w));
   SetLength(S, W);         // <<<<< add
    f.ReadBuffer(s[1], w);
    Memo1.Lines.Text := s;
  except
    {...}
  end;

That initialises the string in memory as a chunk of memory to receive the bytes
read. Otherwise there is no space for the read bytes.

Usually when you code ...

S := 'Some Text';

... then Delphi initialises the string for you in the background, busing it as
a buffer you have to do it yourself.

Alan Lloyd
alangll...@aol.com

Other Threads