无法使用std :: wifstream从文件中读取 [英] Cannot read from a file using std::wifstream

查看:118
本文介绍了无法使用std :: wifstream从文件中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一个文件使用16位字符,因为我在Visual Studio 2017的二进制编辑器中看到过它。所以我使用std :: wifstream来打开它。然而,当我用getline阅读时,我得到垃圾(一堆符号看起来像日本人那样)。

I know a file uses 16 bit chars because I have seen it in the binary editor of Visual Studio 2017. So I am using std::wifstream to open it. However when I read with getline, I get garbage (bunch of symbols looking japanese or so).

然后我试着灌输一个像这个:

Then I tried to imbue a facet like this:

std::wifstream in{ m_headerFile.c_str() };

// apply facet
in.imbue(std::locale(in.getloc(), new std::codecvt_utf16<wchar_t, 0x10ffff, std::little_endian>));

这很有效(现在我得到了wchar_t字符但没有"日语"符号。

and this worked (now I get wchar_t characters but no "japanese" symbols.

问题是我不知道为什么我需要灌输这个方面,特别是为什么这个方面。这是一种试验和错误。我原本预计文件的读取将是微不足道的,但它不是' t。顺便说一句,该文件是一个文本文件(当我创建一个MFC应用程序时,Visual Studio 2017实际生成了
),这很奇怪......

The problem is I don't know why I needed to imbue this facet and why this one in particular. It was sort of trial and error. I had expected that the reading of the file would be trivial but it wasn't. By the way, the file is a  text file (actually generated by Visual Studio 2017 when I created an MFC app), which is very strange...

有人可以向我解释这里发生了什么吗?另外,有没有办法让这个fie给你使用的编码,所以我可以用正确的编码打开它?

Can someone explain to me what is happening here? Also, is there a way to ASK the fie to give you the encoding it uses so I can open it with the correct encoding?

看,我从这个文件复制到另一个,当我尝试编译另一个作为我的解决方案的一部分时,我得到这样的问题:

See, I copy from this file to  another and when I try to compile the other as part of my solution, well I get problems like this:

谢谢,

Juan

Juan Dent

Juan Dent

推荐答案

8/29/2018 1:53 PM,Juan Dent写道:

On 8/29/2018 1:53 PM, Juan Dent wrote:


我知道一个文件使用16位字符,因为我在Visual Studio 2017的二进制编辑器中看到过它。所以我使用的是std :: wifstream打开它。然而,当我用getline阅读时,我得到了垃圾(一堆符号看起来像日本人一样)。
$




然后我试着灌输这样的方面:
$


I know a file uses 16 bit chars because I have seen it in the binary editor of Visual Studio 2017. So I am using std::wifstream to open it. However when I read with getline, I get garbage (bunch of symbols looking japanese or so).


Then I tried to imbue a facet like this:

std::wifstream in{ m_headerFile.c_str() };

// apply facet
in.imbue(std::locale(in.getloc(), new std::codecvt_utf16<wchar_t, 0x10ffff, std::little_endian>));



这个有用(现在我得到wchar_t字符,但没有"日语"符号。

问题是我不知道为什么我需要灌输这个方面,特别是为什么这个方面。


and this worked (now I get wchar_t characters but no "japanese" symbols.
The problem is I don't know why I needed to imbue this facet and why this one in particular.

'wifstream'中的'w'指的是流向调用者报告的字符。它与文件的内容无关,并不意味着文件数据的任何特定编码。两者之间的映射是通过
codecvt facet安装的因此,您需要安装一个与文件数据的实际编码相匹配的转换器。

The 'w' in 'wifstream' refers to the characters the stream reports to the caller. It has nothing to do with the contents of the file, doesn't imply any particular encoding of the file data. The mapping between the two is performed by codecvt facet installed into the stream's locale. So therefore, you need to install a converter that matches the actual encoding of the file data.


此外,是否有一种方式来询问fie给你它使用的编码,所以我可以用正确的编码打开它?

Also, is there a way to ASK the fie to give you the encoding it uses so I can open it with the correct encoding?

如果你很幸运,那就有一个字节 - 开头的订单标记(BOM) - 您可以使用它来进行猜测。否则,不是真的 - 相同的字节序列可能很好地代表一个vali d文本有许多不同的编码 - 当然每个文本都有不同的文字。
您应该通过文件本身外部的某些方式知道编码。

If you are lucky, there's a byte-order mark (BOM) at the beginning - you can use that to make a guess. Otherwise, not really - the same sequence of bytes may very well represent a valid text in many different encodings - different text in each, of course. You are supposed to know the encoding by some means external to the file itself.


这篇关于无法使用std :: wifstream从文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆