By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,165 Members | 894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,165 IT Pros & Developers. It's quick & easy.

StreamReader Encoding ansi problem

P: n/a
Hi,
I've got a problem using StreamReader in VB.NET.
I try to read a txt file that contains extended characters and theese
are removed from the line that is being read.

I've read a lot of articles about ANSI encoding like this
http://support.microsoft.com/default...b;en-us;889835

but System.Text.Encoding.Default don't work!!

Any ideas??
Thanks, LucaJonny!

Nov 21 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
LucaJonny schrieb:
Hi,
I've got a problem using StreamReader in VB.NET.
I try to read a txt file that contains extended characters and
theese are removed from the line that is being read.

I've read a lot of articles about ANSI encoding like this
http://support.microsoft.com/default...b;en-us;889835

but System.Text.Encoding.Default don't work!!

Any ideas??
Thanks, LucaJonny!

Are you sure the file is an ANSI encoded text file?
Armin
Nov 21 '05 #2

P: n/a
Try setting the Encoding parameter of your StreamReader to "Default". I
mean, not the "default" which you get if you leave it blank, but the
actual word "Default". If you system default encoding (mine is, for
example, Western European (Windows)) can handle extended characters,
your problem is solved.
At least, that solved it for me.

Cheers,
zdrakec

Nov 21 '05 #3

P: n/a
Ah, sorry, I see you are using "Default". I mean to say, of course,
check your system's default setting to make sure it can handle the
extended character set.

Sorry,

zdrakec

Nov 21 '05 #4

P: n/a
Ah, sorry, I see you are using "Default". I mean to say, of course,
check your system's default setting to make sure it can handle the
extended character set.

Sorry,

zdrakec

Nov 21 '05 #5

P: n/a
I don't understand!
if I open the file with NOTEPAD (in the open file dialog the ANSI
decoding is setting as default) it seem OK!

if I see in the system setting the international and language options,
there isn't an ANSI western Europe encoding, but only an ANSI central
Europe encoding.

this is the problem???
if so, why NOTEPAD (and other editor software) read well the file??
how I install the ANSI western Europe encoding??

Thanks, LucaJonny!!

zdrakec ha scritto:
Ah, sorry, I see you are using "Default". I mean to say, of course,
check your system's default setting to make sure it can handle the
extended character set.

Sorry,

zdrakec


Nov 21 '05 #6

P: n/a
"LucaJonny" <lu********@gmail.com> schrieb
Hi,
I've got a problem using StreamReader in VB.NET.
I try to read a txt file that contains extended characters and
theese are removed from the line that is being read.

I've read a lot of articles about ANSI encoding like this
http://support.microsoft.com/default...b;en-us;889835

but System.Text.Encoding.Default don't work!!

Any ideas??
Thanks, LucaJonny!

What do you mean with "extended" characters? Which char codes? Can you show
us the code how you read the file? How do you see that there are "removed"
characters?
Armin

Nov 21 '05 #7

P: n/a
as "extended" characters I mean:






this is my code:

Dim srStreamReader As StreamReader
Dim sFile As String = "C:\Temp\MyText.txt"
Dim sFileText As String

srStreamReader = New StreamReader(sFile, Encoding.Default)
sFileText = srStreamReader.ReadToEnd()
srStreamReader.Close()

Dim swStreamWriter As TextWriter = New StreamWriter(sFile & ".new")
swStreamWriter.Write(sFileText)
swStreamWriter.Close()

C:\Temp\MyText.txt seem like:

This is my text file.
In Italian "citt" means city
The european currency is EURO ()
.....
......
.......

C:\Temp\MyText.txt.new seem like:

This is my text file.
In Italian "citt " means city
The european currency is EURO (,)
.....
......
.......

Thanks


Armin Zingler ha scritto:
"LucaJonny" <lu********@gmail.com> schrieb
Hi,
I've got a problem using StreamReader in VB.NET.
I try to read a txt file that contains extended characters and
theese are removed from the line that is being read.

I've read a lot of articles about ANSI encoding like this
http://support.microsoft.com/default...b;en-us;889835

but System.Text.Encoding.Default don't work!!

Any ideas??
Thanks, LucaJonny!

What do you mean with "extended" characters? Which char codes? Can you show
us the code how you read the file? How do you see that there are "removed"
characters?


Armin


Nov 21 '05 #8

P: n/a
"LucaJonny" <lu********@gmail.com> schrieb
as "extended" characters I mean:




?

this is my code:

Dim srStreamReader As StreamReader
Dim sFile As String = "C:\Temp\MyText.txt"
Dim sFileText As String

srStreamReader = New StreamReader(sFile, Encoding.Default)
sFileText = srStreamReader.ReadToEnd()
srStreamReader.Close()

Dim swStreamWriter As TextWriter = New StreamWriter(sFile & ".new")
swStreamWriter.Write(sFileText)
swStreamWriter.Close()

C:\Temp\MyText.txt seem like:

This is my text file.
In Italian "citt" means city
The european currency is EURO (?)
....
.....
......

C:\Temp\MyText.txt.new seem like:

This is my text file.
In Italian "citt " means city
The european currency is EURO (,)
....
.....
......

Thanks

You wrote that the file that you *read* does not contain the extended
characters. I still don't see how you check this. If you read sFileText from
the stream and display it in a the debug window or in a messagebox or a
textbox, are the characters there? If they are there, there is no problem.
As you don't speciy the 'Default' encoding when *writing* the file,
'mytext.txt.new' is UTF-8 encoded.
Armin

Nov 21 '05 #9

P: n/a
Hey Luca:

Try this:

Dim inputString As String = ""
Dim byteArray() As Byte
Dim inputStream As FileStream
inputStream = New FileStream("pathtosomefile", FileMode.Open,
FileAccess.Read)
ReDim byteArray(CInt(inputStream.Length) - 1)
inputStream.Read(byteArray, 0, CInt(inputStream.Length) - 1)
inputStream.Close()
Dim x As Integer
For x = LBound(byteArray) To UBound(byteArray)
inputString &= Chr(byteArray(x))
Next 'x

You will find that "inputString" has the characters that StreamReader
disregarded.

Hope this is helpful,
zdrakec

Nov 21 '05 #10

P: n/a
LucaJonny,
| srStreamReader = New StreamReader(sFile, Encoding.Default)
| sFileText = srStreamReader.ReadToEnd()

The sFileText variable more then likely contains your extended characters,
as Armin suggests, use the Debug window in VS to verify.

| Dim swStreamWriter As TextWriter = New StreamWriter(sFile & ".new")
| swStreamWriter.Write(sFileText)
Ah! There's the Rub!!

You wrote the file in UTF-8 instead of ANSI, all your "extended characters"
now have a different encoding.

If you want to write in your Ansi encoding as defined by your Windows
Control Panel, you need to include Encoding.Default on the StreamWriter
also.

Dim swStreamWriter As TextWriter = New StreamWriter(sfile & ".new",
False, Encoding.Default)
swStreamWriter.Write(sFileText)
Remember both StreamReader & StreamWriter default to UTF-8 encoding, instead
of the Ansi encoding defined by the regional settings under your Windows
Control Panel.

Hope this helps
Jay
"LucaJonny" <lu********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
as "extended" characters I mean:




?

this is my code:

Dim srStreamReader As StreamReader
Dim sFile As String = "C:\Temp\MyText.txt"
Dim sFileText As String

srStreamReader = New StreamReader(sFile, Encoding.Default)
sFileText = srStreamReader.ReadToEnd()
srStreamReader.Close()

Dim swStreamWriter As TextWriter = New StreamWriter(sFile & ".new")
swStreamWriter.Write(sFileText)
swStreamWriter.Close()

C:\Temp\MyText.txt seem like:

This is my text file.
In Italian "citt" means city
The european currency is EURO (?)
.....
......
.......

C:\Temp\MyText.txt.new seem like:

This is my text file.
In Italian "citt " means city
The european currency is EURO (,)
.....
......
.......

Thanks


Armin Zingler ha scritto:
"LucaJonny" <lu********@gmail.com> schrieb
Hi,
I've got a problem using StreamReader in VB.NET.
I try to read a txt file that contains extended characters and
theese are removed from the line that is being read.

I've read a lot of articles about ANSI encoding like this
http://support.microsoft.com/default...b;en-us;889835

but System.Text.Encoding.Default don't work!!

Any ideas??
Thanks, LucaJonny!

What do you mean with "extended" characters? Which char codes? Can you
show
us the code how you read the file? How do you see that there are "removed"
characters?
Armin

Nov 21 '05 #11

P: n/a
I'm so stupid,
I read in ANSI and wrote in UTF-8!!!!

Now it's all right!!
Thanks All!
Jay B. Harlow [MVP - Outlook] ha scritto:
LucaJonny,
| srStreamReader = New StreamReader(sFile, Encoding.Default)
| sFileText = srStreamReader.ReadToEnd()

The sFileText variable more then likely contains your extended characters,
as Armin suggests, use the Debug window in VS to verify.

| Dim swStreamWriter As TextWriter = New StreamWriter(sFile & ".new")
| swStreamWriter.Write(sFileText)
Ah! There's the Rub!!

You wrote the file in UTF-8 instead of ANSI, all your "extended characters"
now have a different encoding.

If you want to write in your Ansi encoding as defined by your Windows
Control Panel, you need to include Encoding.Default on the StreamWriter
also.

Dim swStreamWriter As TextWriter = New StreamWriter(sfile & ".new",
False, Encoding.Default)
swStreamWriter.Write(sFileText)
Remember both StreamReader & StreamWriter default to UTF-8 encoding, instead
of the Ansi encoding defined by the regional settings under your Windows
Control Panel.

Hope this helps
Jay
"LucaJonny" <lu********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
as "extended" characters I mean:




?

this is my code:

Dim srStreamReader As StreamReader
Dim sFile As String = "C:\Temp\MyText.txt"
Dim sFileText As String

srStreamReader = New StreamReader(sFile, Encoding.Default)
sFileText = srStreamReader.ReadToEnd()
srStreamReader.Close()

Dim swStreamWriter As TextWriter = New StreamWriter(sFile & ".new")
swStreamWriter.Write(sFileText)
swStreamWriter.Close()

C:\Temp\MyText.txt seem like:

This is my text file.
In Italian "citt" means city
The european currency is EURO (?)
....
.....
......

C:\Temp\MyText.txt.new seem like:

This is my text file.
In Italian "citt " means city
The european currency is EURO (,)
....
.....
......

Thanks


Armin Zingler ha scritto:
"LucaJonny" <lu********@gmail.com> schrieb
Hi,
I've got a problem using StreamReader in VB.NET.
I try to read a txt file that contains extended characters and
theese are removed from the line that is being read.

I've read a lot of articles about ANSI encoding like this
http://support.microsoft.com/default...b;en-us;889835

but System.Text.Encoding.Default don't work!!

Any ideas??
Thanks, LucaJonny!

What do you mean with "extended" characters? Which char codes? Can you
show
us the code how you read the file? How do you see that there are "removed"
characters?
Armin


Nov 21 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.