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

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.