468,769 Members | 1,931 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,769 developers. It's quick & easy.

Problem with accents i.e. é when reading a text file

Hi I have a trivial question for the experts.

I would lilke to read be able to read a text file that could contain french
characters with accents.
I'm opening the file with the File.openText(...) and
then loop through the file with the Readline function into a string variable.
But everytime a hit a char with an accent I loses it.

Example : père reads pre

Thanks in advance

Chérif

Jul 21 '05 #1
3 7984
Cherif Diallo <Ch**********@discussions.microsoft.com> wrote:
Hi I have a trivial question for the experts.

I would lilke to read be able to read a text file that could contain french
characters with accents.
I'm opening the file with the File.openText(...) and
then loop through the file with the Readline function into a string variable.
But everytime a hit a char with an accent I loses it.


You need to specify the correct encoding. File.OpenText reads the text
using a UTF-8 encoding, which is probably not what you want. Use
StreamReader to open the file and read the text using the correct
encoding. What is the encoding of your file?

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information about encodings.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
If you know that your system is set up with a code page capable of showing
French accented letters, you can use System.Text.Encoding.Default with
StreamReader (as Jon suggested) which will give you the encoding for this
code page. This will work even if your files are not saved in one of the
unicode formats.

Regards, Jakob.
"Jon Skeet [C# MVP]" wrote:
Cherif Diallo <Ch**********@discussions.microsoft.com> wrote:
Hi I have a trivial question for the experts.

I would lilke to read be able to read a text file that could contain french
characters with accents.
I'm opening the file with the File.openText(...) and
then loop through the file with the Readline function into a string variable.
But everytime a hit a char with an accent I loses it.


You need to specify the correct encoding. File.OpenText reads the text
using a UTF-8 encoding, which is probably not what you want. Use
StreamReader to open the file and read the text using the correct
encoding. What is the encoding of your file?

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information about encodings.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3
Thanks a lot Guys.

It works like a charm.

"Jakob Christensen" wrote:
If you know that your system is set up with a code page capable of showing
French accented letters, you can use System.Text.Encoding.Default with
StreamReader (as Jon suggested) which will give you the encoding for this
code page. This will work even if your files are not saved in one of the
unicode formats.

Regards, Jakob.
"Jon Skeet [C# MVP]" wrote:
Cherif Diallo <Ch**********@discussions.microsoft.com> wrote:
Hi I have a trivial question for the experts.

I would lilke to read be able to read a text file that could contain french
characters with accents.
I'm opening the file with the File.openText(...) and
then loop through the file with the Readline function into a string variable.
But everytime a hit a char with an accent I loses it.


You need to specify the correct encoding. File.OpenText reads the text
using a UTF-8 encoding, which is probably not what you want. Use
StreamReader to open the file and read the text using the correct
encoding. What is the encoding of your file?

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information about encodings.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by fabrice | last post: by
14 posts views Thread by Job Lot | last post: by
8 posts views Thread by Phil Slater | last post: by
4 posts views Thread by Nina | last post: by
4 posts views Thread by Amit Maheshwari | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.