all groups > dotnet windows forms > may 2007 >
You're in the

dotnet windows forms

group:

Missing characters when reading from a file


Missing characters when reading from a file Gaël_Rosset
5/30/2007 3:41:40 PM
dotnet windows forms:
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 ?

Re: Missing characters when reading from a file Morten Wennevik [C# MVP]
5/30/2007 8:08:15 PM
[quoted text, click to view]

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!
Re: Missing characters when reading from a file Jon Skeet [C# MVP]
5/30/2007 11:14:12 PM
[quoted text, click to view]

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
Re: Missing characters when reading from a file Gaël_Rosset
5/30/2007 11:16:12 PM
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);
}




[quoted text, click to view]
Re: Missing characters when reading from a file Gaël_Rosset
5/31/2007 12:00:00 AM
The code is a utility class for both .NET 2.0 and .NET CF which does not
support File.ReadAllLines() unfortunately...


[quoted text, click to view]
AddThis Social Bookmark Button