Home All Groups Group Topic Archive Search About
Author
11 Oct 2005 10:47 PM
lorenzo.viscanti
Hi everybody, I'm trying to create some text files in c#. I have not to
put the encoding preamble bytes at the beginning of the files, 'cause
the unix software that reads my files gets some troubles with the
preamble.
How cna i do that?I know that using StreamWriter will alwais generate
those bytes...
Thanks
Lorenzo

Author
12 Oct 2005 6:19 AM
Jon Skeet [C# MVP]
<lorenzo.visca***@gmail.com> wrote:
> Hi everybody, I'm trying to create some text files in c#. I have not to
> put the encoding preamble bytes at the beginning of the files, 'cause
> the unix software that reads my files gets some troubles with the
> preamble.
> How cna i do that?I know that using StreamWriter will alwais generate
> those bytes...

No, it will only generate the preamble if you use an encoding which
includes the preamble. If you don't specify the encoding, StreamWriter
uses a UTF-8 encoding which doesn't include the preamble. If for
whatever reason you need to specify the encoding, you can create a new
instance of UTF8Encoding which doesn't include the preamble - just use
new UTF8Encoding(false)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
15 Oct 2005 5:37 PM
Richard Grimes [MVP]
lorenzo.visca***@gmail.com wrote:
> Hi everybody, I'm trying to create some text files in c#. I have not
> to put the encoding preamble bytes at the beginning of the files,
> 'cause the unix software that reads my files gets some troubles with
> the preamble.
> How cna i do that?I know that using StreamWriter will alwais generate
> those bytes...
> Thanks
> Lorenzo

If you want to use the existing framework code you can simply derive
from the class and override GetPreamble to return a zero length array
(new byte[0])

Richard
Author
15 Oct 2005 6:26 PM
Jon Skeet [C# MVP]
Show quote
<"Richard Grimes [MVP]" <read my sig>> wrote:
> lorenzo.visca***@gmail.com wrote:
> > Hi everybody, I'm trying to create some text files in c#. I have not
> > to put the encoding preamble bytes at the beginning of the files,
> > 'cause the unix software that reads my files gets some troubles with
> > the preamble.
> > How cna i do that?I know that using StreamWriter will alwais generate
> > those bytes...
> > Thanks
> > Lorenzo
>
> If you want to use the existing framework code you can simply derive
> from the class and override GetPreamble to return a zero length array
> (new byte[0])

You don't need to do that though - both of the built-in encodings which
can generate preambles (Unicode and UTF-8) have constructors which let
you tell the new instance not to generate a preamble.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
16 Oct 2005 10:46 AM
Richard Grimes
Jon Skeet [C# MVP] wrote:
> You don't need to do that though - both of the built-in encodings
> which can generate preambles (Unicode and UTF-8) have constructors
> which let you tell the new instance not to generate a preamble.

Ahh, that is useful. Thanks for pointing it out.

Richard

AddThis Social Bookmark Button