Problem with printing to text file

I am completely stumped. I have a program that retrieves information stored in a database, assigns the database values ​​to the appropriate class. The class has a print method that prints the values ​​of its fields. Users enter either the order ID or the date range of the order, and then it finds those orders and prints them out in a text file. The problem I am running into is that once in a while, depending on which id and date you choose, the text file will throw garbage out this way. "〢Ⱒⰱ 潋 瑲 刬 瑵 慨 湮 圬 扥 † '慍 歲 瑥 㔬 ㈶‸ ⁗ ㈱ 琰 瑓 敲 瑥 Ⱜ 汁 楳 Ɒ 䱉 㘬 㠰 ㌰" I don't know at all, because when I debug it, all values render correctly in class, print method, and result set. What could be the problem? Has anyone had this with them?

OS - Windows Vista, the language I am using is C #. I am using StreamWriter class with FileStream as parameter.

This only happens with some orders. For example, if I select order 7 or order 1-6 it works great, but if I select order 1-7 I get weird language. ????

thank

+1


source to share


2 answers


Based on your comment what happens is that Notepad is trying to guess the encoding of the file. See how it's done .

You must explicitly specify the file encoding.



From the documentation :

By default, StreamWriter uses UTF8Encoding instance unless otherwise specified. This UTF8Encoding instance is built without a Byte Order Mark (BOM), so its GetPreamble method returns an empty byte array. To create a StreamWriter using UTF-8 encoding and BOM, consider using a constructor that specifies the encoding, such as StreamWriter (String, Boolean, Encoding).

+4


source


which looks like a bad conversion to / from Unicode or a given and incorrectly applied culture variable.



+2


source







All Articles