¿ character added by My.Computer.FileSystem.WriteAllText

M

Manuel

Whenever I create/append a file using:

My.Computer.FileSystem.WriteAllText

It adds a ¿ character at the beginning of the file. Anyone knows why is
this happening?
 
H

Herfried K. Wagner [MVP]

Manuel said:
Whenever I create/append a file using:

My.Computer.FileSystem.WriteAllText

It adds a ¿ character at the beginning of the file. Anyone knows why is
this happening?

Maybe a BOM is written at the beginning of the file (untested):

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

How do you determine that the character is added at the beginning of the
file?
 
M

Manuel

Maybe a BOM is written at the beginning of the file (untested):
I think this is the case because it only happens when I use a large string
to create/append the file. How can I turn it off?
How do you determine that the character is added at the beginning of the
file?

I open it with notepad and the character is there =p
 
M

Manuel

btw, it can be reproduced creating a large string and writing it to a file.
For example:
------------------------------------
Dim X As String = ""
Dim I As Integer
For I = 1 To 3000
Dim c As String = ""
Dim h As Integer
For h = 1 To 80
c &= "X"
Next
X &= c & vbCrLf
Next
My.Computer.FileSystem.WriteAllText("Test.txt", X, False)
------------------------------------

Manuel said:
Maybe a BOM is written at the beginning of the file (untested):
I think this is the case because it only happens when I use a large string
to create/append the file. How can I turn it off?
How do you determine that the character is added at the beginning of the
file?

I open it with notepad and the character is there =p
 
M

Michael D. Ober

Using Manual's code, I got the following as the first three bytes of the
file

0xEF 0xBB 0xBF

Then the expected output of 80 characters lines of "X" were output. When I
did a debug.print on Left(X, 1) before the output, I received the expected
"X". I just report this to the VS 2005 beta team.

Mike Ober.

Manuel said:
btw, it can be reproduced creating a large string and writing it to a file.
For example:
------------------------------------
Dim X As String = ""
Dim I As Integer
For I = 1 To 3000
Dim c As String = ""
Dim h As Integer
For h = 1 To 80
c &= "X"
Next
X &= c & vbCrLf
Next
My.Computer.FileSystem.WriteAllText("Test.txt", X, False)
 
M

Michael D. Ober

MS Response: "By Design"

Description: Opened by Mike Ober on 2005-08-16 at 06:44:43

Execute the following code:

Module Module1

Sub Main()
Dim X As String = ""
Dim I As Integer
For I = 1 To 3000
Dim c As String = ""
Dim h As Integer
For h = 1 To 80
c &= "X"
Next
X &= c & vbCrLf
Next
Debug.Print(Left$(X, 1))
My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectorie
s.Desktop & "\Test.txt", X, False)
End Sub

End Module

The debug.print(Left$(X,1)) returns the expected "X". However, the first
three characters output to test.txt are "0xEF 0xBB 0xBF".

Version from the about box:
Microsoft Visual Studio 2005
Version 8.0.50215.44 (beta2.050215-4400)


Edited by Microsoft on 2005-08-18 at 10:06:23

Thank you for submitting this issue. I'm passing it to the feature team to
take a look.

-Shamez
Resolved as By Design by Microsoft on 2005-08-18 at 13:10:13

Thank you very much for submitting this issue.

This behavior is by design. As noted on Visual Studio 2005 Beta
Documentation
(http://msdn2.microsoft.com/library/27t17sxs(en-us,vs.80).aspx): "When no
encoding is specified, UTF-8 will be used.". The first 3 bytes you saw is
the Byte Order Mark (BOM) of UTF8. If you want to write the text out using
ASCII encoding, without any BOM, you can pass in System.Text.Encoding.ASCII
as the 4th parameter of WriteAllText.

Best regards,
Huy Nguyen, Visual Basic team.

Steps to Reproduce: Run the above code and view the output file in a binary
viewer. I used the VC 6++ editor from VS 6.
Actual Results: Binary view of output file's first line is
0xEF 0xBB 0xBF followed by 80 "X" characters as expected.

The rest of the file is correct.

Expected Results: 3000 lines of 80 "X" characters with a CRLF terminating
each line.
Attachments:
Workarounds:
Primary:
Duplicates:


==========
Somewhat counter intuitive for those of use used to standard ASCII, but in a
web and international language world, it is reasonable.

Mike.
 

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