Newbie question on Writing to a file

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
 
U

UAError

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
 
M

Martin Maat [EBL]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top