Missing characters when reading from a file

  • Thread starter =?ISO-8859-1?Q?Ga=EBl_Rosset?=
  • Start date
?

=?ISO-8859-1?Q?Ga=EBl_Rosset?=

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
 
M

Morten Wennevik [C# MVP]

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.
 
?

=?ISO-8859-15?Q?Ga=EBl_Rosset?=

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);
}



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.
 
J

Jon Skeet [C# MVP]

Gaël Rosset said:
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.
 
?

=?ISO-8859-1?Q?Ga=EBl_Rosset?=

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

Gaël Rosset said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top