[fpc-devel] Method for write string into TStream

Michael Van Canneyt michael at freepascal.org
Mon Jul 21 12:21:25 CEST 2014

On Mon, 21 Jul 2014, LacaK wrote:

> Sven Barth  wrote / napísal(a):
>       Am 21.07.2014 10:29 schrieb "LacaK" <lacak at zoznam.sk>:
>       >
>       > Hi,
>       > When I work with f.e. TFileStream I need often write string into stream.
>       > Now I use:
>       >  Stream1.WriteBuffer(s[1], length(s));
>       >
>       > I think, that for convenience it will be useful having something like:
>       > procedure TStream.WriteString(const s: string);
>       >  begin
>       >    WriteBuffer(s[1], length(s)*sizeof(char);
>       >  end;
>       >
>       > In FPC we now have for user convenience also WriteByte, WriteWord, WriteQWord (which all calls WriteBuffer)
>       > and Delphi has lot of overloaded WriteData (but none of them takes as parameter string):
>       >  http://docwiki.embarcadero.com/Libraries/XE3/en/System.Classes.TStream.WriteData
>       >
>       > Do you think, that something similar can be added also in FPC ?
>       There is already a WriteAnsiString ( http://www.freepascal.org/docs-html/rtl/classes/tstream.writeansistring.html ) with the difference that it also writes out the length.
> Yes I know, and I do not want write length only content of string
>       Adding an additional overload (especially with a name like WriteString) would IMHO be confusing because of this.
> Yes without looking into documentation at first look it can by confusable
> (what about WriteRawString then) ?
>       Nothing stops you though from writing a class helper that adds a WriteString method to TStream.
> Yes, I can create also own descendant etc. ... but I am asking for general solution, which will be useful for all.

We can simply add a boolean parameter 'WriteLength : Boolean = True' to the existing call. 
It keeps backwards compatibility, and allows you to write the strings with or without length.

Another option is to add a WriteLn() with various overloaded variants. It should be clear then that this will write a textual representation.


More information about the fpc-devel mailing list