gw.Write(string) ?

  • Thread starter Thread starter dinny
  • Start date Start date
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()
 
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>
 
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
 
Back
Top