Results 1 to 7 of 7

Thread: accents removed when reading from text file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    accents removed when reading from text file

    i am reading a text file line by line like this

    VB Code:
    1. dim reader As StreamReader = New StreamReader("c:\myfile.txt")
    2. Do While Not reader.EndOfStream
    3.   msbox reader.readline
    4. loop

    the accents characters are removed..
    e.g.

    instead of displaying
    "González"

    it displays
    "Gonzlez"

    any idea why??

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: accents removed when reading from text file

    It has got to do with the encoding when reading in text. You have to specify the encoding type. Like this:

    VB Code:
    1. Dim myString() As String = System.IO.File.ReadAllLines("C:\Temp\Try1.txt", System.Text.Encoding.UTF7)
    2.         For i As Integer = 0 To myString.Length - 1
    3.             MessageBox.Show(myString(i))
    4.         Next
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    Re: accents removed when reading from text file

    ok thx.. i used to use filesystemobject in vb6 and tried it on vb 2005 too and it's working fine.....

  4. #4
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: accents removed when reading from text file

    By default, the system.io classes (I like to call them 'tools') use Unicode as encoding when reading. When saving they normally use the same encoding that has been used for reading or the encoding that was last used internally on a string.

    If your app will be used on computers with different regional settings, the least complicated approach is to resave all external chunks of text (.txt, .doc, .xls etc) in unicode. This way, even chinese text opened in Notepad on a polish PC will keep its symbols. Same goes for VB programs.

    I once thought that the whole encoding stuff is a total chaos but after reading this great tutorial I at least understand the chaos

    Unfortunately, XP and possibly all other OSes in the family are not Unicode enabled, so without some third party external patching you will have missing accents and other formatting in messageboxes, file names and so on IF the computer is not set up with the respective regional settings.
    VB 2005, Win Xp Pro sp2

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    Re: accents removed when reading from text file

    the unicode stuff... etc..i dun understand it very well.... ... i am always confused with it...

  6. #6
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: accents removed when reading from text file

    As I said, the differences are explained very well here:
    http://www.joelonsoftware.com/articles/Unicode.html

    You will hardly ever have a problem after reading it.
    VB 2005, Win Xp Pro sp2

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    Re: accents removed when reading from text file

    Ok thanks will try to find a courage to read it ty

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width