读取 Unicode 文件 C++

发布于 2024-07-21 20:36:24 字数 398 浏览 8 评论 0原文

我有一个简单的问题要问。 我有一个以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

掀纱窥君容 2024-07-28 20:36:24

您可以使用 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.

妥活 2024-07-28 20:36:24

由于您很赶时间,请以二进制模式使用 ifstream 并完成您的工作。 我和你遇到了同样的问题,这拯救了我的一天。 (当然,这不是推荐的解决方案,它只是一个 hack)

  ifstream file; 
  file.open("k:/test.txt", ifstream::in|ifstream::binary);

  wchar_t buffer[2048]; 
  file.seekg(2);
  file.read((char*)buffer, line_length);
  wprintf(L"%s\n", buffer);
  file.close();

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)

  ifstream file; 
  file.open("k:/test.txt", ifstream::in|ifstream::binary);

  wchar_t buffer[2048]; 
  file.seekg(2);
  file.read((char*)buffer, line_length);
  wprintf(L"%s\n", buffer);
  file.close();
蓝梦月影 2024-07-28 20:36:24

对于它的价值,我想我已经读到您必须使用允许您指定编码的 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

因为看清所以看轻 2024-07-28 20:36:24

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文