2

I create a CSV file from a C# application but the characters  are displayed in Excel and OpenOfficeCalc in the first cell but not in Notepad and Notepad++.

Here is my code:

StreamWriter streamWriter = new StreamWriter(new FileStream(filePath, FileMode.Create), Encoding.UTF8);
List<MyData> myData = GetMyData;

foreach(MyData md in myData)
{
    streamWriter.WriteLine(md.Date + "," + md.Data1 + "," + md.Data2 + "," + md.Data3 + "," + md.Data4);
}
streamWriter.Flush();
streamWriter.Close();

MyData is

public struct MyData
{
    public float Data1;
    public float Data2;
    public float Data3;
    public float Data4;
    public DateTime Date;
}

Here is the result in Notepad and Notepad++:

01/12/2010 00:04:00,0.08,78787.4,9.1,5
01/12/2010 00:09:00,0.07,78787.42,9.1,5
01/12/2010 00:14:00,0.06,78787.44,9.1,5
01/12/2010 00:19:00,1.45,78787.58,9.1,5
01/12/2010 00:24:00,2.13,78788.15,9.1,5
01/12/2010 00:29:00,1.72,78788.53,9,5
01/12/2010 00:34:00,0.89,78788.73,9,5

And in Excel and Calc:

01/12/2010 00:04:00   0.08    78787.4     9.1    5
01/12/10 00:09           0.07    78787.42    9.1    5
01/12/10 00:14           0.06    78787.44    9.1    5
01/12/10 00:19           1.45    78787.58    9.1    5
01/12/10 00:24           2.13    78788.15    9.1    5
01/12/10 00:29           1.72    78788.53    9      5
01/12/10 00:34           0.89    78788.73    9      5

Those 3 characters appears only once at the start of the file and then everything is as it should be.

My question is:

Where does  come from and how to remove it?

I have tried to write my output in a StringBuilder and to debug to see its content and it does not have those character.

RenardNoir
  • 45
  • 1
  • 5

4 Answers4

5

This is the BOM for UTF8 encoding

Look at this question: Write text files without Byte Order Mark (BOM)?

Community
  • 1
  • 1
jdehaan
  • 19,700
  • 6
  • 57
  • 97
2

I think that is BOM (unicode header). Specify ANSI encoding in constructor of StreamWriter. It is simple: Just set it to Encoding.Default.

update: If you must use UTF-8 and need to get rid of those 3 bytes at the beginning, use this: new UTF8Encoding(false). This way the writer will stay in UTF8, but won't write BOM.

Al Kepp
  • 5,831
  • 2
  • 28
  • 48
  • OK, then you need to stay in UTF8 and omit BOM. I updated my answer with information how to do it. I hope it helps. – Al Kepp Feb 08 '11 at 12:32
2

You may want to specify the encoding type based on your input. StreamWriter.Encoding Property

http://msdn.microsoft.com/en-us/library/system.io.streamwriter.encoding.aspx

RaM
  • 1,126
  • 10
  • 25
1

Try to specify Encoding.ASCII instead of using Encoding.UTF8 when you create the StreamWriter

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115