Newbie question on Writing to a file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I am writing to file using a StreamWriter in a Windows Form application
I must write different records with a specific length
However, I have more bytes at the end (in my file) than the number of character that I wrote
Why is that? Isn't each letter take one byte
If not, is there a specific way to specify that we want to use one byte only for each character

thanks a lo

Paula
 
Paula said:
Hi,

I am writing to file using a StreamWriter in a Windows Form application.
I must write different records with a specific length.
However, I have more bytes at the end (in my file) than the number of character that I wrote!
Why is that? Isn't each letter take one byte?
If not, is there a specific way to specify that we want to use one byte only for each character?

thanks a lot

Paula

Make sure that

StreamWriter.Encoding = Encoding.ASCII
or
StreamWriter.Encoding = Encoding.UTF8
or
StreamWriter.Encoding = Encoding.UTF7

sw.WriteLine() will add an additional newline
(usually "\r\n," this can be modified through the
StreamWriter.NewLine property).

NET Framework Class Library: StreamWriter Class
http://msdn.microsoft.com/library/d.../html/frlrfsystemiostreamwriterclasstopic.asp


If you need a finer level of control consider using BinaryWriter and the Encoding class

NET Framework Class Library: BinaryWriter Members
http://msdn.microsoft.com/library/d...tml/frlrfsystemiobinarywritermemberstopic.asp

..NET Framework Class Library: Encoding Methods
http://msdn.microsoft.com/library/d.../html/frlrfSystemTextEncodingMethodsTopic.asp
 
Hi,

I am writing to file using a StreamWriter in a Windows Form application.
I must write different records with a specific length.
However, I have more bytes at the end (in my file) than the number of character that I wrote!
Why is that? Isn't each letter take one byte?

Unicode is the default: double byte characters. You should leave it that way
if you don't have a very good reason to persist your text as single byte
characters.

The System.Text namespace has classes to convert from one encoding type to
another. If you really want to store text in something different than
Unicode, check out the classes in this namespace (ASCIIEncoding, Decoder,
Encoder, Encoding, UnicodeEncoding, UTF7Encoding, UTF8Encoding). They work
in a very powerful way in conjunction with streams, you'd better look for an
example because it may not be obvious how to hook things up at first.

Martin.
 
Back
Top