skitron

Hello all - my first post here. I'm a VB newbie building an app that relies on generating csv files and later reading them into a SQLServerCE 3.5b2 table.

While debugging, I discovered that all csv files generated include hex: ef bb bf at beginning of string and this is causing havoc when attempting to read the file into the table (works fine if I edit them out first).

I narrowed things down to these statements used in the generation of the csv which provokes the issue:

Dim dataStr As New System.Text.StringBuilder()

My.Computer.FileSystem.WriteAllText("test.txt", dataStr.ToString(), False)

This code will produce an 'empty' txt file except for the hex values ef bb bf (which are not viewable in notepad). Using these statements in a larger block of code to build the csv results in an otherwise perfect csv except for the same leading hex values.

I tried using dataStr.Remove to strip the hex no avail. So my questions are:

1) Is this 'normal behavior', a bug, or am I doing something wrong

2) How can I avoid this or at least strip the 'trash' off the front of these files

Thanks in advance for any help!

Regards,

skitron

BTW - this is true in both VB Express 2005 sp1 and 2008 b2



Re: Visual Basic Express Edition 'Stringbuilder' issue or question - not sure which?

ReneeC

Those three bytes are called the BOF for beginning of file.

I've had to strip them off before and am supplying the relevant pieces of the console program I wrote as a striping utility.

In this code, replace the commandline args with a filename string.

Try
bR = New FileStream(Environment.GetCommandLineArgs(1), FileMode.Open)
Catch ex As Exception
Console.Write("Cannot open " + Environment.GetCommandLineArgs(1) + " for read")
Console.Write(ex.Message)
Exit Sub
End Try

Infilelen = bR.Length
Dim buffer(bR.Length - 1) As Byte
Try
bR.Read(buffer, 0, bR.Length)
Catch ex As Exception
Console.Write("Could not read " + Environment.GetCommandLineArgs(1))
Console.Write(ex.Message)
Exit Sub
End Try

bR.Close()
bR.Dispose()

If buffer(0) <> &HEF Or buffer(1) <> &HBB Then

Console.Write(Environment.GetCommandLineArgs(1) + _

" does not have signature.")

Exit Sub

End If

Try

System.IO.File.Delete(Environment.GetCommandLineArgs(1))

Console.Write(Environment.GetCommandLineArgs(1) + " deleted.")

Catch ex As Exception

Console.Write("Error deleting " + Environment.GetCommandLineArgs(1))

Console.Write(ex.Message)

Exit Sub

End Try

bW = New FileStream(Environment.GetCommandLineArgs(1), _

FileMode.CreateNew, FileAccess.Write)

bW.Write(buffer, 3, Infilelen - 3)

bW.Close()

Console.Write("Three signature bytes stripped from: " +

Environment.GetCommandLineArgs(1))

bW.Dispose()






Re: Visual Basic Express Edition 'Stringbuilder' issue or question - not sure which?

skitron

Thank you so much!! I'll give it a whirl and report back.





Re: Visual Basic Express Edition 'Stringbuilder' issue or question - not sure which?

ReneeC

OKeeeeeeeeeeeee

I omitted a little and have added to the original post to correct that.






Re: Visual Basic Express Edition 'Stringbuilder' issue or question - not sure which?

skitron

Hehehe, glad I waited till the morning and rechecked the thread before trying it!

A bit of tweaking and it drops right in my VB window app and works like a champ. I found that for whatever reason, my process to build the csv's also appends hex 0d 0a at the end (I suppose some sort of file end marker ) but not problem, I was able to deal with it by simply adjusting:

bW.Write(buffer, 3, Infilelen - 3)

to:

bW.Write(buffer, 3, Infilelen - 5)

...and now the trash at both ends of the csv is gone.

Thanks again so much for your help, it is greatly appreciated.

Regards,

skitron

[EDIT] turns out the hex 0d 0a are the cr/linefeeds so they can stay...





Re: Visual Basic Express Edition 'Stringbuilder' issue or question - not sure which?

Chris Dunaway

The reason you get those characters at the beginning of your file is that the default encoding used when calling WriteAllText is UTF8. If you pass Encoding.Default as the fourth paramter to WriteAllText, the characters will not be there and you won't have to go to the trouble of stripping them off.

Chris





Re: Visual Basic Express Edition 'Stringbuilder' issue or question - not sure which?

skitron

I figured there had to be something like this, thanks a million it works like a champ! I can see now the value of this forum for all involved.

Regards,

skitron