开发者

How to find whether the stream has an unicode

I am having a file name "Connecticut is now 2 °C.txt" which contains a unicode but the file contents are just normal characters. Previously the code was used to identify whether the file name has unicode if so the file header was written with the unicode details. This way of implementation leads to conflict in the output file. So can anyone 开发者_如何转开发suggest how to find whether the file stream has an unicode in it.

Thanks in advance,

Lokesh.


By far the simplest strategy is to decide on an encoding for a particular file, e.g. UTF-8, and use it exclusively, both when you write it and then when you read it. Trying to detect what encoding is in use is decidedly error prone so it's best not to have to do this detection.


UPDATE

In the comments below you clarify that you wish to write to a file that is created by somebody else with an unknown encoding.

In full generality this is impossible to do with 100% reliability.

If you are lucky then you may find that the file comes with a Byte Order Mark (BOM). In which case you can read the BOM and thus infer the encoding. There's no requirement for a text file to contain a BOM and they frequently don't.

However, I would urge you to agree an interchange format with whoever is creating these files. Pick a single encoding and always use it.


I think this link would be helpful for you. Pay attention to IsTextUnicode Function

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜