• <bdo id='MyiHw'></bdo><ul id='MyiHw'></ul>
    <i id='MyiHw'><tr id='MyiHw'><dt id='MyiHw'><q id='MyiHw'><span id='MyiHw'><b id='MyiHw'><form id='MyiHw'><ins id='MyiHw'></ins><ul id='MyiHw'></ul><sub id='MyiHw'></sub></form><legend id='MyiHw'></legend><bdo id='MyiHw'><pre id='MyiHw'><center id='MyiHw'></center></pre></bdo></b><th id='MyiHw'></th></span></q></dt></tr></i><div id='MyiHw'><tfoot id='MyiHw'></tfoot><dl id='MyiHw'><fieldset id='MyiHw'></fieldset></dl></div>

      1. <tfoot id='MyiHw'></tfoot>

        <small id='MyiHw'></small><noframes id='MyiHw'>

      2. <legend id='MyiHw'><style id='MyiHw'><dir id='MyiHw'><q id='MyiHw'></q></dir></style></legend>

        Shift-JIS 解码在 Visual C++ 2013 中使用 wifstrem 失败

        Shift-JIS decoding fails using wifstrem in Visual C++ 2013(Shift-JIS 解码在 Visual C++ 2013 中使用 wifstrem 失败)

              <tbody id='MSVxf'></tbody>
            • <small id='MSVxf'></small><noframes id='MSVxf'>

              • <bdo id='MSVxf'></bdo><ul id='MSVxf'></ul>

                1. <tfoot id='MSVxf'></tfoot>
                  <legend id='MSVxf'><style id='MSVxf'><dir id='MSVxf'><q id='MSVxf'></q></dir></style></legend>
                  <i id='MSVxf'><tr id='MSVxf'><dt id='MSVxf'><q id='MSVxf'><span id='MSVxf'><b id='MSVxf'><form id='MSVxf'><ins id='MSVxf'></ins><ul id='MSVxf'></ul><sub id='MSVxf'></sub></form><legend id='MSVxf'></legend><bdo id='MSVxf'><pre id='MSVxf'><center id='MSVxf'></center></pre></bdo></b><th id='MSVxf'></th></span></q></dt></tr></i><div id='MSVxf'><tfoot id='MSVxf'></tfoot><dl id='MSVxf'><fieldset id='MSVxf'></fieldset></dl></div>
                  本文介绍了Shift-JIS 解码在 Visual C++ 2013 中使用 wifstrem 失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 std::wifstream 和 std::getline 读取以 Shift-JIS (cp 932) 编码的文本文件.以下代码在 VS2010 中有效,但在 VS2013 中失败:

                  I am trying to read a text file encoded in Shift-JIS (cp 932) using std::wifstream, and std::getline. The following code works in VS2010 but fails in VS2013:

                  std::wifstream in;
                  in.open("data932.txt");
                  
                  const std::locale locale(".932");
                  
                  in.imbue(locale);
                  
                  std::wstring line1, line2;
                  std::getline(in, line1);
                  std::getline(in, line2);
                  const bool good = in.good();
                  

                  该文件包含多行,其中第一行仅包含 ASCII 字符,第二行是日语脚本.因此,当此代码段运行时,line1 应包含 ASCII 行,line2 日文脚本,good 应为 true.

                  The file contains several lines, where the first line contains just ASCII characters, and the second is Japanese script. Thus, when this snippet runs, line1 should contain the ASCII line, line2 the Japanese script, and good should be true.

                  在VS2010中编译时,结果如预期.但是在VS2013编译时,line1包含ASCII行,但line2为空,good为false.

                  When compiled in VS2010, the result is as expected. But when compiled in VS2013, line1 contains the ASCII line, but line2 is empty, and good is false.

                  我调试到 CRT 中(因为 Visual Studio 提供了源),发现在两个版本之间修改了一个名为 _Mbrtowc(在文件 xmbtowc.c 中)的内部函数,并且改变了检测双字节字符前导字节的方式,VS 2013中检测前导字节失败,从而无法解码字节流.

                  I debugged into the CRT, (as the source is provided with Visual Studio), and found that an internal function called _Mbrtowc (in file xmbtowc.c) was modified between the two versions, and the way they use to detect a lead byte of a double byte character was changed, and the one in VS 2013 fails to detect a lead byte, thus fails to decode the byte stream.

                  进一步的调试揭示了一个点,其中一个 _Cvtvec 对象的 _Isleadbyte 数组被初始化(在函数 _Getcvt() 中,在文件 xwctomb.c),并且初始化会产生错误的结果.它似乎总是使用代码页 1252,这是我系统上的默认代码页,而不是为正在使用的流设置的 932.但是,我无法确定它是否是设计使然,并且我错过了一些获得良好结果所需的步骤,或者这确实是 VS2013 的 CRT 中的错误.

                  Further debugging revealed a point, where a _Cvtvec object's _Isleadbyte array is initialized (in the function _Getcvt(), in file xwctomb.c), and that initialization produces a wrong result. It seems that it always uses code page 1252, which is the default code page on my system, and not 932 which is set for the stream in use. However, I could not decide if it is by design, and I missed some required steps to get a good result, or this is indeed a bug in the CRT for VS2013.

                  很遗憾我没有安装 VS2012,所以我无法在那个版本上进行测试.

                  Unfortunately I don't have VS2012 installed, so I could not test on that version.

                  欢迎对此主题有任何见解!

                  Any insights on this topic are welcome!

                  推荐答案

                  我找到了一个解决方法:如果为了创建语言环境我显式更改了全局 MBC 代码页,语言环境被正确初始化,并且行被读取并按预期解码.

                  I have found a workaround: if for the creation of the locale I explicitly change the global MBC code page, the locale is initialized correctly, and the lines are read and decoded as expected.

                  const int oldMbcp = _getmbcp();
                  _setmbcp(932);
                  const std::locale locale("Japanese_Japan.932");
                  _setmbcp(oldMbcp);
                  

                  这篇关于Shift-JIS 解码在 Visual C++ 2013 中使用 wifstrem 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                      <bdo id='7Fz6K'></bdo><ul id='7Fz6K'></ul>

                          <small id='7Fz6K'></small><noframes id='7Fz6K'>

                        1. <tfoot id='7Fz6K'></tfoot>
                        2. <i id='7Fz6K'><tr id='7Fz6K'><dt id='7Fz6K'><q id='7Fz6K'><span id='7Fz6K'><b id='7Fz6K'><form id='7Fz6K'><ins id='7Fz6K'></ins><ul id='7Fz6K'></ul><sub id='7Fz6K'></sub></form><legend id='7Fz6K'></legend><bdo id='7Fz6K'><pre id='7Fz6K'><center id='7Fz6K'></center></pre></bdo></b><th id='7Fz6K'></th></span></q></dt></tr></i><div id='7Fz6K'><tfoot id='7Fz6K'></tfoot><dl id='7Fz6K'><fieldset id='7Fz6K'></fieldset></dl></div>

                              <tbody id='7Fz6K'></tbody>
                            <legend id='7Fz6K'><style id='7Fz6K'><dir id='7Fz6K'><q id='7Fz6K'></q></dir></style></legend>