Hello,

I have the following reader function :

public static string[] fileReadAllLines(string strFileName)
{
ArrayList content = new ArrayList();

using (StreamReader sr = File.OpenText(strFileName))
{
string input;
while ((input = sr.ReadLine()) != null)
{
content.Add(input);
}
sr.Close();
}

return toStringArray(content);
}

which also can be replaced with :

public static string[] fileReadAllLines(string strFileName)
{
return File.ReadAllLines(strFileName);
}

Both those methods remove all special characters (é,è,ø,æ,...) from the
stream, what to do to get them ?

Thanks

Re: Missing characters when reading from a file by Morten

Morten
Wed May 30 13:08:15 CDT 2007

On Wed, 30 May 2007 15:41:40 +0200, Gaël Rosset <gael@tele2adsl.dk> wrote:

> Hello,
>
> I have the following reader function :
>
> public static string[] fileReadAllLines(string strFileName)
> {
> ArrayList content = new ArrayList();
>
> using (StreamReader sr = File.OpenText(strFileName))
> {
> string input;
> while ((input = sr.ReadLine()) != null)
> {
> content.Add(input);
> }
> sr.Close();
> }
>
> return toStringArray(content);
> }
>
> which also can be replaced with :
>
> public static string[] fileReadAllLines(string strFileName)
> {
> return File.ReadAllLines(strFileName);
> }
>
> Both those methods remove all special characters (é,è,ø,æ,...) from the
> stream, what to do to get them ?
>
> Thanks
>

Hi,

The StreamReader uses UTF-8 encoding if not told otherwise. In addition, File.OpenText is meant for UTF-8 encoded files.
Try changing your code to something like

using(FileStream fs = File.Open(strFileName, ...))
{
using(StreamReader sr = new StreamReader(fs, Encoding.Default))
{
...
}
}

You don't have to close the StreamReader since the using statement will do it for you.
--
Happy coding!
Morten Wennevik [C# MVP]

Re: Missing characters when reading from a file by gael

gael
Wed May 30 16:16:12 CDT 2007

This worked :-)
Thanks for your help Morten.

public static string[] fileReadAllLines(string strFileName)
{

ArrayList content = new ArrayList();
using (FileStream fs = File.Open(strFileName,
FileMode.Open, FileAccess.Read))
{
using (StreamReader sr = new StreamReader(fs,
Encoding.Default))
{
string input;
while ((input = sr.ReadLine()) != null)
{
content.Add(input);
}
}
}
return toStringArray(content);
}




Morten Wennevik [C# MVP] wrote:
> On Wed, 30 May 2007 15:41:40 +0200, Gaël Rosset <gael@tele2adsl.dk> wrote:
>
>> Hello,
>>
>> I have the following reader function :
>>
>> public static string[] fileReadAllLines(string strFileName)
>> {
>> ArrayList content = new ArrayList();
>>
>> using (StreamReader sr = File.OpenText(strFileName))
>> {
>> string input;
>> while ((input = sr.ReadLine()) != null)
>> {
>> content.Add(input);
>> }
>> sr.Close();
>> }
>>
>> return toStringArray(content);
>> }
>>
>> which also can be replaced with :
>>
>> public static string[] fileReadAllLines(string strFileName)
>> {
>> return File.ReadAllLines(strFileName);
>> }
>>
>> Both those methods remove all special characters (é,è,ø,æ,...) from the
>> stream, what to do to get them ?
>>
>> Thanks
>>
>
> Hi,
>
> The StreamReader uses UTF-8 encoding if not told otherwise. In addition, File.OpenText is meant for UTF-8 encoded files.
> Try changing your code to something like
>
> using(FileStream fs = File.Open(strFileName, ...))
> {
> using(StreamReader sr = new StreamReader(fs, Encoding.Default))
> {
> ...
> }
> }
>
> You don't have to close the StreamReader since the using statement will do it for you.

Re: Missing characters when reading from a file by Jon

Jon
Wed May 30 17:14:12 CDT 2007

Ga=EBl Rosset <gael@tele2adsl.dk> wrote:
> public static string[] fileReadAllLines(string strFileName)
> {
>=20
> ArrayList content =3D new ArrayList();
> using (FileStream fs =3D File.Open(strFileName,=20
> FileMode.Open, FileAccess.Read))
> {
> using (StreamReader sr =3D new StreamReader(fs,=20
> Encoding.Default))
> {
> string input;
> while ((input =3D sr.ReadLine()) !=3D null)
> {
> content.Add(input);
> }
> }
> }
> return toStringArray(content);
> }

Just out of interest, are you using .NET 1.1 or 2.0? 2.0 has=20
File.ReadAllLines(string, Encoding) which does the same thing, I=20
believe.

--=20
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Re: Missing characters when reading from a file by gael

gael
Thu May 31 02:11:52 CDT 2007

The code is a utility class for both .NET 2.0 and .NET CF which does not
support File.ReadAllLines() unfortunately...


Jon Skeet [C# MVP] wrote:
> Gaël Rosset <gael@tele2adsl.dk> wrote:
>> public static string[] fileReadAllLines(string strFileName)
>> {
>>
>> ArrayList content = new ArrayList();
>> using (FileStream fs = File.Open(strFileName,
>> FileMode.Open, FileAccess.Read))
>> {
>> using (StreamReader sr = new StreamReader(fs,
>> Encoding.Default))
>> {
>> string input;
>> while ((input = sr.ReadLine()) != null)
>> {
>> content.Add(input);
>> }
>> }
>> }
>> return toStringArray(content);
>> }
>
> Just out of interest, are you using .NET 1.1 or 2.0? 2.0 has
> File.ReadAllLines(string, Encoding) which does the same thing, I
> believe.
>