whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

32 Visitors Online


 
...use TMemoryStream to save data?
Autor: Damian Gorski
[ Print tip ]  

Tip Rating (10):  
     


{Think of memory stream as a file that is located in memory.  So the writes
are very similar to the write command for files.  (Actually it is closer to
the blockwrite command.)}

{To put a string the slow way you could do the following:}

    
for i := 1 to Length(s) do memstream.Write(s[i], 1);

{That would write the string one character at a time.  Simple and easy to
understand, but a bit slow.  A faster way would be to do the following:}

    
memstream.Write(s[1], Length(s));

{The two lines do the same thing, they append characters to the stream. If
you have never done a seek on the stream, they just append to the end.}

{Now to handle the line feeds you have to add them yourself:}

    
memstream.Write(#13, 1);
    memstream.Write(#10, 1);

{Or you could do some sneaky things like this:}

    
procedure StreamWriteStr(var ms: TMemoryStream; s: string);
    begin
        
ms.Write(s[1], Length(s));
    end;

    procedure StreamWriteLnStr(var ms: TMemoryStream; s: string);
    begin
        
StreamWriteStr(ms, s + #13#10);
    end;

{Or you could create you own descendant class of TMemoryStream with a method
to write strings.}

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners