0

I am trying to read a character (£) from a text file, using the following code.

public static List<string> ReadAllLines(string path, bool discardEmptyLines, bool doTrim) 
{
   var retVal = new List<string>();
   if (string.IsNullOrEmpty(path) || !File.Exists(path)) {
        Comm.File.Log.LogError("ReadAllLines", string.Format("Could not load file: {0}", path));
            return retVal;
   }

   //StreamReader sr = null;
   StreamReader sr = new StreamReader(path, Encoding.Default));

   try {
     sr = File.OpenText(path);

     while (sr.Peek() >= 0) {
         var line = sr.ReadLine();

         if (discardEmptyLines && (line == null || string.IsNullOrEmpty(line.Trim()))) {
            continue;
         }

         if (line != null) {
            retVal.Add(doTrim ? line.Trim() : line);
         }
      }
   } 
   catch (Exception ex) {
            Comm.File.Log.LogGeneralException("ReadAllLines", ex);
   }
   finally {
      if (sr != null) {
           sr.Close();
      }
   }

   return retVal;
}

But my code is not correctly reading £, It is reading the character as please guide me what needs to be done to read the special character.

Thanks in advance.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sara
  • 93
  • 1
  • 4
  • 17
  • Well, the file you are reading is not in Default encoding as you are opening it. You should open it with appropriate encoding, and it will work. This is a great read on the topic: http://www.joelonsoftware.com/articles/Unicode.html – bosnjak Mar 28 '14 at 14:43
  • 1
    Looks like an encoding issue. Probably, the file is an ANSI file, and is read like a UTF-8 file. – GolezTrol Mar 28 '14 at 14:43
  • @GolezTrol: Actually it is read an ANSI file... – Guffa Mar 28 '14 at 14:44
  • If you read a UTF-8 file as if it were ANSI, you would have two or three weird characters instead of a single one. – GolezTrol Mar 28 '14 at 14:54

4 Answers4

1

The file you are reading is not encoded the same as Encoding.Default. It is likely UTF-8. Try using UTF-8 for this particular file. For more generic usage, you should see Determining the Encoding of a text file.

Community
  • 1
  • 1
selkathguy
  • 1,171
  • 7
  • 17
0

Look like a encoding problem. Try creating your StreamReader with a UTF-8 (or Unicode) encoding instead of default.

StreamReader sr = new StreamReader(path, Encoding.UTF8));

0

Try to replace Encoding.Default with Encoding.GetEncoding(437)

Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31
0

Encoding information can be provided to stream reader in 2 ways.

1)Save your file with save as option and select the appropriate encoding option from dropdown in windows. see screenshot 2)If your files are dynamic in nature use Encoding.GetEncoding() with StreamReader