473,322 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

WriteAllText

Jim
I'm parsing a file and writing a text file with cr/lf's on the end of
line. It works fine EXCEPT I get a hex FF FE as the first two
characters of the file.

1)How do I get rid of them
2)How did they get there
3)What did I do wrong?

Jim

Here's the code:
===============================
dim counter as string = ""

Using MyReader As New
Microsoft.VisualBasic.FileIO.TextFieldParser(openF ileDialog1.FileName)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters("~")
counter = "0000*"
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String = ""
For Each currentField In currentRow
If VB.Left(currentField, 2) = "ST" Then
counter = VB.Right(currentField, 4) & "*"
End If
If VB.Left(currentField, 2) = "SE" Then
counter = "0000*"
End If
currentField = counter & currentField & vbCrLf
My.Computer.FileSystem.WriteAllText("C:/testfile.txt",
currentField, True)
Next
Catch ex As VB.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & _
"is not valid and will be skipped.")
End Try
End While
End Using
Mar 18 '07 #1
1 3538
On Mar 17, 8:21 pm, Jim <joSmith...@RemoveThisStuffNetscape.net>
wrote:
I'm parsing a file and writing a text file with cr/lf's on the end of
line. It works fine EXCEPT I get a hex FF FE as the first two
characters of the file.
FFFE... That's the Unicode Byte Order Marker (BOM). Reading the
docs, says that if you don't specify the encoding, then UTF-8 is used
(hence, you get the BOM at the begining of the file). So, it must be
calling System.IO.File.WriteAllText underneath, and explicitly passing
System.Text.Encoding.UTF8. File.WriteAllText uses UTF8 by default -
but, if you don't pass the encoding, then it doesn't insert the BOM.

I couldn't help it - I just looked at this in Reflector, and sure
enough, it explicitly passes UTF8 to the File.WriteAllText method.
That explains the BOM.

Just add System.Text.Encoding.ASCII to your call and it should
disappear.

--
Tom Shelton

Mar 18 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joy M | last post by:
Hi - On page 78 of the book Build a program Now! Microsoft Visual Basic 2005 Express Edition I was following the instructions, the book told me to insert code using code snippets. However...
1
by: John Wright | last post by:
I am running a console application that connects to an Access database (8 million rows) and converts it to a text file and then cleans and compacts the database. When it runs I get the following...
2
by: =?Utf-8?B?am9obmFicmFoYW0xMDE=?= | last post by:
hello, I'm trying to write a some text to a text file using WriteAllText method. This method works fine but I don't understand how can I use format specifiers to format the text as we use in C i.e...
6
by: kimiraikkonen | last post by:
Hi, I want to save all the item content of a listbox line by line into a simple text file then recall them when my project is opened. For example listbox1 contains: That - item1 Group ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.