473,889 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

StreamReader Encoding ansi problem

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.Enc oding.Default don't work!!

Any ideas??
Thanks, LucaJonny!

Nov 21 '05
11 31748
LucaJonny,
| srStreamReader = New StreamReader(sF ile, Encoding.Defaul t)
| 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(sF ile & ".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.Defaul t on the StreamWriter
also.

Dim swStreamWriter As TextWriter = New StreamWriter(sf ile & ".new",
False, Encoding.Defaul t)
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********@gma il.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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(sF ile, Encoding.Defaul t)
sFileText = srStreamReader. ReadToEnd()
srStreamReader. Close()

Dim swStreamWriter As TextWriter = New StreamWriter(sF ile & ".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********@gma il.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.Enc oding.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
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(sF ile, Encoding.Defaul t)
| 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(sF ile & ".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.Defaul t on the StreamWriter
also.

Dim swStreamWriter As TextWriter = New StreamWriter(sf ile & ".new",
False, Encoding.Defaul t)
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********@gma il.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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(sF ile, Encoding.Defaul t)
sFileText = srStreamReader. ReadToEnd()
srStreamReader. Close()

Dim swStreamWriter As TextWriter = New StreamWriter(sF ile & ".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********@gma il.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.Enc oding.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
2797
by: aurora | last post by:
I have long find the Python default encoding of strict ASCII frustrating. For one thing I prefer to get garbage character than an exception. But the biggest issue is Unicode exception often pop up in unexpected places and only when a non-ASCII or unicode character first found its way into the system. Below is an example. The program may runs fine at the beginning. But as soon as an unicode character u'b' is introduced, the program boom...
12
3978
by: Tamir Khason | last post by:
I have Windows Form application recieved data from clipboard and convert its encoding based on some ruls. So doing following: //from source to multiple targets System.Text.Encoding targ1 = Encoding.GetEncoding("Target_1_code_name"); System.Text.Encoding targ2 = Encoding.GetEncoding("Target_2_code_name"); System.Text.Encoding source = Encoding.GetEncoding("Source_code_name"); byte sourceBytes = source.GetBytes(source); byte targ1Bytes =...
0
3329
by: elliott | last post by:
Hello, I'm trying to use a C# IHttpModule to install a Response filter however IIS keeps removing the Content-Encoding header from the response; but all the other headers I set stay intact. So far I have tested my code on IIS 5.1, 6.0 and ASP.NET Development Server/9.0.0.0 all with the same result. Does anyone have any suggestions?
0
9810
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11202
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10792
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10443
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5830
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6029
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4650
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.