gw.Write(string) ?

D

dinny

I am getting an extra character at te beginning of the file,
can someone please check if there is an error in my code?

Dim fs As FileStream
fs = New FileStream(Form1.REGFILE, FileMode.CreateNew, FileAccess.Write)
Dim gw As New BinaryWriter(fs)
gw.Write(s)
gw.Flush()
gw.Close()
fs.Close()
 
H

Herfried K. Wagner [MVP]

dinny said:
I am getting an extra character at te beginning of the file,
can someone please check if there is an error in my code?

Dim fs As FileStream
fs = New FileStream(Form1.REGFILE, FileMode.CreateNew, FileAccess.Write)
Dim gw As New BinaryWriter(fs)
gw.Write(s)
gw.Flush()
gw.Close()
fs.Close()

Maybe the character is an UTF BOM:

<URL:http://www.unicode.org/faq/utf_bom.html>
 
J

Jay B. Harlow [MVP - Outlook]

Dinny,
Review the help for BinaryWriter.Write(String)!

Seeing as BinaryWriter is writing a Binary File, it encodes the length of
the string before it writes the encoded characters of the string. It does
this so as to allow reading back the same number of characters in
BinaryReader.ReadString.

For information on the encoded length see the following:

BinaryWriter.Write(String):
http://msdn.microsoft.com/library/d...rlrfSystemIOBinaryWriterClassWriteTopic13.asp


Corresponding BinaryReader.ReadString:
http://msdn.microsoft.com/library/d...fSystemIOBinaryReaderClassReadStringTopic.asp

Hope this helps
Jay
 

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

Similar Threads


Top