读取 Unicode 文件 C++
我有一个简单的问题要问。 我有一个以 FFFE 开头的 UTF 16 文本文件要读取。 有哪些C++工具可以处理这种文件? 我只想阅读它,过滤一些行,然后显示结果。
它看起来很简单,但我只有处理普通 ascci 文件的经验,而且我很着急。 我正在使用 VS C++,但我不想使用托管 C++。
问候
这里放了一个非常简单的例子
wifstream file;
file.open("C:\\appLog.txt", ios::in);
wchar_t buffer[2048];
file.seekg(2);
file.getline(buffer, bSize-1);
wprintf(L"%s\n", buffer);
file.close();
I have a simple question to ask. I have a UTF 16 text file to read wich starts with FFFE. What are the C++ tools to deal with this kind of file? I just want to read it, filter some lines, and display the result.
It looks simple, but I just have experience in work with plain ascci files and I'm in the hurry. I'm using VS C++, but I'm not want to work with managed C++.
Regards
Here a put a very simple example
wifstream file;
file.open("C:\\appLog.txt", ios::in);
wchar_t buffer[2048];
file.seekg(2);
file.getline(buffer, bSize-1);
wprintf(L"%s\n", buffer);
file.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 fgetws,读取 16 位字符。 您的文件采用小端字节顺序。 由于 x86 机器也是小端字节序,因此您应该能够轻松处理该文件。 当您想要进行输出时,请使用 fwprintf。
另外,我同意更多信息可能有用。 例如,您可能正在使用一个抽象出其中一些内容的库。
You can use fgetws, which reads 16-bit characters. Your file is in little-endian,byte order. Since x86 machines are also little-endian you should be able to handle the file without much trouble. When you want to do output, use fwprintf.
Also, I agree more information could be useful. For instance, you may be using a library that abstracts away some of this.
由于您很赶时间,请以二进制模式使用 ifstream 并完成您的工作。 我和你遇到了同样的问题,这拯救了我的一天。 (当然,这不是推荐的解决方案,它只是一个 hack)
Since you are in the hurry, use ifstream in binary mode and do your job. I had the same problems with you and this saved my day. (it is not a recommended solution, of course, its just a hack)
对于它的价值,我想我已经读到您必须使用允许您指定编码的 Microsoft 函数。
http://msdn.microsoft.com/en-us /library/z5hh6ee9(VS.80).aspx
For what it's worth, I think I've read you have to use a Microsoft function which allows you to specfiy the encoding.
http://msdn.microsoft.com/en-us/library/z5hh6ee9(VS.80).aspx
FFFE只是最初的BOM(字节顺序标记)。 就像平常一样从文件中读取,但读取到宽字符缓冲区中。
The FFFE is just the initial BOM (byte order mark). Just read from the file like you normally do, but into a wide char buffer.