Question Importing Text - Accents replaced with question marks

samboy29

Member
Joined
Oct 2, 2008
Messages
8
Programming Experience
Beginner
Hello, I'm a bit stuck trying to figure out how to import data from a text doc without losing accents marks (currently they are all being converted into question mark icons). The text file being retrieved is fine so I figure I need to add encoding support(?), just have had no luck figuring out how to implement it. Thanks for all help in advance =)

VB.NET:
Dim FileText As String
            Dim FileLines() As String
            Dim Separators(1) As String
            Dim SeparatorChar As String
            Separators(0) = Environment.NewLine
            Separators(1) = ControlChars.Tab


            Dim i As Integer
            ' Check that file exists
            If My.Computer.FileSystem.FileExists(Route) Then
                ' Read the whole file to a string
                FileText = My.Computer.FileSystem.ReadAllText(Route)
                ' Split to lines
                FileLines = FileText.Split(CChar(Environment.NewLine))
                ' Now FileLines array has all the lines from the file
                
                SeparatorChar = "   "
                ' Loop the lines

                For i = 0 To FileLines.GetUpperBound(0)

                    OneLine = FileLines(i).Split(Separators(1))
                    Word.L1(i) = OneLine(0)
                Next i
 
First up, there's no point calling ReadAllText and then Split when you can call IO.File.ReadAllLines to do both in one call. Further, given that you are then breaking up each line, I would suggest that using a TextFieldParser would be a better idea still. It will let you read a file line by line and automatically break each one up as it reads it. The MSDN documentation for the class has examples.

Regardless of which option you choose, they all allow you to specify an Encoding object to be used to convert the bytes in the file to text. If you are not getting the results you expect then the Default encoding that you are implicitly using is obviously wrong. You would most likely need to use Encoding.UTF8 but there are other options too. Read the documentation for the type/method you choose top use and you'll see how to specify the Encoding.
 
Back
Top