Accent problem

  • Thread starter Thread starter John Devlon
  • Start date Start date
J

John Devlon

Hi,

Can anyone please help me?

I've wrote a small application that reads the content from a text-file.
Some characters are not displayed correcly.

I noticed the text-file uses different types of Accent Signs (').

Is there some simple methode to replace all of them to just one kind ?

thanx

Kind regards

John
 
John Devlon said:
Hi,

Can anyone please help me?

I've wrote a small application that reads the content from a
text-file. Some characters are not displayed correcly.

I noticed the text-file uses different types of Accent Signs (').

Is there some simple methode to replace all of them to just one kind
?

thanx


You must use the correct encoding when reading from the file. When creating
a StreamReader pass the correct System.Text.Encoding instance to it's
constructor.


Armin
 
Hi,

I added the code below i'm using. I'm using an array instead of an
arraylist. It's not something i'm proud off, but it works...
Where do I have to add the encoding ?

John



Private arrContentFile() As String
Dim oFile As System.IO.File
Dim oRead As System.IO.StreamReader

Private Sub ReadfileTest(ByVal strFilename As String)
Dim intCount As Int64 = 0
Dim strLine As String

Dim objStreamReader = New System.IO.StreamReader(strFilename)

strLine = objStreamReader.ReadLine

Do While Not strLine Is Nothing
If Not (IsNothing(arrContentFile)) Then
intCount = arrContentFile.GetUpperBound(0) + 1
End If
ReDim Preserve arrContentFile(intCount)
arrContentFile(intCount) = strLine

strLine = objStreamReader.ReadLine
Loop

objStreamReader.Close()

End Sub
 
John Devlon said:
Hi,

I added the code below i'm using. I'm using an array instead of an
arraylist. It's not something i'm proud off, but it works...
Where do I have to add the encoding ?

John



Private arrContentFile() As String
Dim oFile As System.IO.File
Dim oRead As System.IO.StreamReader

Private Sub ReadfileTest(ByVal strFilename As String)
Dim intCount As Int64 = 0
Dim strLine As String

Dim objStreamReader = New System.IO.StreamReader(strFilename)

strLine = objStreamReader.ReadLine

Do While Not strLine Is Nothing
If Not (IsNothing(arrContentFile)) Then
intCount = arrContentFile.GetUpperBound(0) +
1 End If
ReDim Preserve arrContentFile(intCount)
arrContentFile(intCount) = strLine

strLine = objStreamReader.ReadLine
Loop

objStreamReader.Close()

End Sub

First, you should enable Option Strict.

Then, as written, the Encoding must be passed to the constructor of the
StreamReader:

Dim objStreamReader As New System.IO.StreamReader( _
strFilename, System.Text.Encoding.Default _
)

I don't know which encoding has been used to write the file, so you first
have to know this and replace "Default" by the encoding to be used.


Side notes:
- You can write "Do Until" instead of "Do While Not"
- The "Is" Operator is more straightforward than calling the IsNothing
function.
- Yes, you could have used an Arraylist. ;-) Or, in FW 2.0, a List(Of
String).



Armin
 
Dear Mr. Zingler,

Thanx for your tips... it was very usefull....

But problem is getting more complex...

The files I'm trying to read are text-file, created in notepad, using the
ANSI encoding...
Resaving a file in notepad to UTF and adjusting the application (encoding
UTF) works great and solves the problem...

However, I can't resave every file... How can I correcly read an ANSI
text-file?

Kind regards,

John
 
Armin Zingler said:
Dim objStreamReader As New System.IO.StreamReader( _
strFilename, System.Text.Encoding.Default _
)

I don't know which encoding has been used to write the file, so you first
have to know this and replace "Default" by the encoding to be used.

Just for the information of the OP: 'Default' refers to the default Windows
ANSI codepage of the system, which is used by default in the Windows editor
on Windows XP, for example. If you do not specify any encoding in the
'StreamReader''s constructor, UTF-8 is assumed and used.
 
John Devlon said:
Dear Mr. Zingler,

Thanx for your tips... it was very usefull....

But problem is getting more complex...

The files I'm trying to read are text-file, created in notepad,
using the ANSI encoding...
Resaving a file in notepad to UTF and adjusting the application
(encoding UTF) works great and solves the problem...

However, I can't resave every file... How can I correcly read an
ANSI text-file?

See Herfried's answer (System.Text.Encoding.Default).


Armin
 
Dear Mr. Zingler,

Your provided solutions works great... Like a charm...

Thank you very much

John
 
Back
Top