Re: Bug in StreamReader.ReadLine()? It reads special chars wrong...

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: David Browne (meat_at_hotmail.com)
Date: 08/02/04


Date: Mon, 2 Aug 2004 17:28:38 -0500


"VMI" <vonchi_m@yahoo.com> wrote in message
news:elBI0nNeEHA.1764@TK2MSFTNGP10.phx.gbl...
> When I execute a ReadLine from an ascii file with special chars (ie. the
> 'Ñ' in "NUÑEZ PEREZ"), it automatically deletes this character. So "NUÑEZ
> PEREZ" becomes "NUEZ PEREZ". How can this be avoided? The reason being
that
> I compare this string to a string in an Access DB (btw, Access also screws
> up with the character by replacing "Ñ" with "-"). So when I compare these
> two strings, they won't match because both systems storing the data
> interpret the char in different ways. The same thing happens with chars
with
> accents (ie. 'ó').
>
> Thanks.
>
>
ASCII is a 7-bit encoding and has no 'Ñ'. In order to have that character,
your file must use an encoding other than ASCII. You must discover what
that encoding is and tell the StreamReader. By default it uses
UTF8Encoding.

Try
  StreamReader sr = new
StreamReader("foo.txt",System.Text.Encoding.Default);

That will us your computer's regional settings to get the current code page,
or figure out what code page is used for your file and specify it.

David



Relevant Pages

  • Re: Questions on possreps
    ... a method for encoding values of type T on some physical medium? ... terminated ASCII string such as "", and must be parsed ... terminated string". ... would make it an "invalid" possrep. ...
    (comp.databases.theory)
  • Re: Unicode to ASCII string conversion
    ... files can be ANSI, ASCII, UTF7, UTF8, EBCDIC, UTF16 and many other ... > to string conversion and explicitly no bytearray, ... >> If you need an ASCII file, then use a ASCII encoding. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Efficient, built-in way to determine if string has non-ASCII chars outside ASCII 32-
    ... I know I can look at the chars of a string individually and compare ... There is no way of telling whether a string includes non-ASCII characters ... string *is* fully ASCII text, you have to check every character, there ...
    (comp.lang.python)
  • Re: string to ascii on line feed
    ... first published ASCII as a standard in 1963. ... refer to multiple things, one of which might be "The encoding Java uses when we ask for the 'ASCII' encoding." ... Conceptually, we have a string in memory, and we wish to store that string to disk, using a specific encoding. ... Now when we say "Encoding FOO is n bits", what we usually mean is either "the encoding uses n bits per character to represent a given string" or the less restrictive "*on average*, the encoding uses n bits per character to represent a given string". ...
    (comp.lang.java.programmer)
  • Re: Writing extended ascii characters to text file.
    ... so in order to get real ASCII codes you should use the GetBytes ... method of an Encoding instance configured for the ASCII encoding (as far as ... again, you've got bytes, not characters. ... > string line; ...
    (microsoft.public.dotnet.languages.csharp)