File encoding

  • Thread starter Thread starter lorenzo.viscanti
  • Start date Start date
L

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
 
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)
 
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
 
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 said:
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
 
Back
Top