Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / May 2007

Tip: Looking for answers? Try searching our database.

Missing characters when reading from a file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gaël Rosset - 30 May 2007 14:41 GMT
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
Morten Wennevik [C# MVP] - 30 May 2007 19:08 GMT
> Hello,
>
[quoted text clipped - 28 lines]
>
> 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.
Signature

Happy coding!
Morten Wennevik [C# MVP]

Gaël Rosset - 30 May 2007 22:16 GMT
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,
>>
[quoted text clipped - 43 lines]
>
> You don't have to close the StreamReader since the using statement will do it for you.
Jon Skeet [C# MVP] - 30 May 2007 23:14 GMT
> public static string[] fileReadAllLines(string strFileName)
>          {
[quoted text clipped - 15 lines]
>              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.

Signature

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

Gaël Rosset - 31 May 2007 08:11 GMT
The code is a utility class for both .NET 2.0 and .NET CF which does not
support File.ReadAllLines() unfortunately...

>> public static string[] fileReadAllLines(string strFileName)
>>          {
[quoted text clipped - 19 lines]
> File.ReadAllLines(string, Encoding) which does the same thing, I
> believe.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.