{"id":185,"date":"2012-10-22T16:44:24","date_gmt":"2012-10-22T08:44:24","guid":{"rendered":"http:\/\/blog.sorayuki.net\/?p=185"},"modified":"2012-10-23T12:21:10","modified_gmt":"2012-10-23T04:21:10","slug":"%e5%9c%a8c%e4%b8%ad%e8%bf%9b%e8%a1%8c%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%bc%96%e7%a0%81%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/blog.sorayuki.net\/?p=185","title":{"rendered":"\u5728C++\u4e2d\u8fdb\u884c\u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362"},"content":{"rendered":"<p>\u6700\u8fd1\u505a\u4e86\u4e2aJubeat Analyzer\u7684\u8c31\u9762\u8f6c\u6210Yubiosi\u8c31\u9762\u7684\u5de5\u5177\u3002\u5728\u5236\u4f5c\u5de5\u5177\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9047\u5230\u4e86\u4e00\u4e2a\u95ee\u9898\uff0c\u5c31\u662f\u6e90\u8c31\u9762\u6587\u4ef6\u4e2d\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u95ee\u9898\u3002<\/p>\n<p>Jubeat Analyzer\u662f\u65e5\u672c\u4eba\u9020\u7684\u8f6f\u4ef6\uff0c\u7136\u540e\u5b83\u4fdd\u5b58\u7684\u8c31\u9762\u53c8\u6ca1\u6709\u7528Unicode\uff0c\u6240\u4ee5\u5728\u4e2d\u6587\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u770b\u5c31\u4f1a\u4e71\u7801\u3002\u5bf9\u4e8e\u7f16\u7801\u8f6c\u6362\u95ee\u9898\uff0c\u5176\u5b9eC++\u662f\u53ef\u4ee5\u76f4\u63a5\u652f\u6301\u7684\u3002\u5bf9\u4e8e\u4e00\u4e2a\u652f\u6301\u8f93\u51fa\u5bbd\u5b57\u7b26\u7684\u6587\u4ef6\u6d41\uff0c\u628a\u533a\u57df\u8bbe\u7f6e\u5230japanese\u5c31\u53ef\u4ee5\u4e86\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre>std::locale jpLoc(\"japanese\");\r\nstd::wfstream fs(\"c:\\\\nanika.txt\", std::ios::in);\r\nfs.imbue(jpLoc);<\/pre>\n<p>\u5982\u679c\u95ee\u9898\u8fd9\u4e48\u7b80\u5355\u5c31\u89e3\u51b3\u7684\u8bdd\u5c31\u597d\u4e86\u2026\u2026<\/p>\n<p>\u95ee\u9898\u662f\uff0c\u6e90\u6587\u4ef6\u4e2d\u4e0d\u4ec5\u4ec5\u5305\u542bShiftJIS\u7f16\u7801\u7684\u90e8\u5206\uff0c\u800c\u4e14\u8fd8\u5305\u542bGBK\u7f16\u7801\u7684\u90e8\u5206\u3002\u6bd4\u5982\u67d0\u4e9b\u5730\u65b9\u7684\u6ce8\u91ca\uff0c\u67d0\u4e9b m=\"\u97f3\u4e50\u6587\u4ef6\u540d\"\u00a0\u7684\u5730\u65b9\u3002\u56e0\u4e3a\u5982\u679c\u97f3\u4e50\u6587\u4ef6\u540d\u7528\u7684\u662fShiftJIS\uff0c\u90a3\u4e48\u5728\u4e2d\u6587\u7cfb\u7edf\u4e0b\u5c31\u6ca1\u529e\u6cd5\u6b63\u5e38\u8bfb\u53d6\u97f3\u4e50\u4e86\u3002<\/p>\n<p>\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6765\u4e00\u4e2aimbue\u4e0d\u80fd\u89e3\u51b3\u95ee\u9898\u3002\u56e0\u4e3a\u5b83\u8bfb\u5230ShiftJIS\u4e0d\u80fd\u8868\u793a\u7684\u5b57\u7b26\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u6d41\u5c31\u7ed9\u4f60eof\u6389\u4e86\u2026\u2026<\/p>\n<p>\u65e2\u7136\u8fd9\u6837\uff0c\u90a3\u6211\u5c31\u4e00\u884c\u4e00\u884c\u8bfb\uff0c\u4e0d\u540c\u60c5\u51b5\u4e0d\u540c\u5904\u7406\u3002\u81ea\u5df1\u8f6c\u6210Unicode\u3002\u597d\u5427\uff0c\u600e\u4e48\u8f6c<\/p>\n<p>\u7528MultiByteToWideChar\u56fa\u7136\u7b80\u5355\uff0c\u4f46\u662f\u6211\u60f3\u8981\u7528C++\u6807\u51c6\u5e93\u53bb\u505a\u800c\u4e0d\u662f\u8c03\u7528\u7cfb\u7edfAPI\u3002\u56e0\u4e3a\u4f60\u770b\uff0cwfstream\u90fd\u53ef\u4ee5\u8bfb\u53d6ShiftJIS\u6587\u4ef6\u5230wstring\uff0c\u90a3\u4e48\u8bf4\u660e\u5185\u90e8\u4e00\u5b9a\u6709\u4ec0\u4e48\u4ee3\u7801\u5b9e\u73b0\u7f16\u7801\u8f6c\u6362\u7684\u3002\u6211\u60f3\u529e\u6cd5\u8c03\u7528\u8fd9\u7f16\u7801\u5c31\u884c\u4e86\u3002<!--more--><\/p>\n<p>\u56e0\u4e3a\u5bbd\u5b57\u7b26\u7684stream\u4e0d\u662f\u53ef\u4ee5\u7528imbue\u53bb\u8bbe\u7f6e\u533a\u57df\u5417\uff0c\u6240\u4ee5\u7b2c\u4e00\u4e2a\u60f3\u5230\u7684\u662f<\/p>\n<pre>std::wstringstream wss;\r\nwss.imbue(jpLoc);\r\nwss &lt;&lt; line;\r\nstd::wstring wline = wss.str();<\/pre>\n<p>\u53ef\u60dc\u8fd9\u6837\u5b50\u6839\u672c\u4e0d\u884c\u3002<\/p>\n<p>\u90a3\u5b83\u5185\u90e8\u5230\u5e95\u8c03\u7528\u4e86\u4ec0\u4e48\u4e1c\u897f\u554a\u3002\u90a3\u6211\u7528\u8c03\u8bd5\u5668\u4e00\u6b65\u4e00\u6b65\u8ddf\u8e2a\uff0cstep in\u00a0\u8fdb\u53bb\uff0c\u770b\u770b\u5b83\u5230\u5e95\u5e72\u4e86\u4ec0\u4e48\u3002\u5199\u4e86\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>std::locale jpLoc(\"japanese\");\r\nstd::wfstream fs(\"c:\\\\nanika.txt\", std::ios::in);\r\nfs.imbue(jpLoc);\r\nstd::wstring wsline;\r\ngetline(fs, wsline);<\/pre>\n<p>\u7136\u540e\u5f00\u59cb\u8c03\u8bd5\u8ddf\u8e2a\u3002\u4e00\u6b65\u6b65\u6df1\u5165\uff0c\u00a0\u5f53\u8c03\u7528\u5806\u6808\u53d8\u6210\u8fd9\u6837\u5b50\u7684\u65f6\u5019\uff1a<\/p>\n<pre>\u5728VS2012\u91cc\uff0c\r\nstd::basic_filebuf&lt;wchar_t,std::char_traits&lt;wchar_t&gt; &gt;::uflow() line 501 C++\r\nstd::basic_filebuf&lt;wchar_t,std::char_traits&lt;wchar_t&gt; &gt;::underflow() line 460 C++\r\nstd::basic_streambuf&lt;wchar_t,std::char_traits&lt;wchar_t&gt; &gt;::sgetc() line 153 C++\r\nstd::getline&lt;wchar_t,std::char_traits&lt;wchar_t&gt;,std::allocator&lt;wchar_t&gt; &gt;(std::basic_istream&lt;wchar_t,std::char_traits&lt;wchar_t&gt; &gt; &amp;&amp; _Istr, std::basic_string&lt;wchar_t,std::char_traits&lt;wchar_t&gt;,std::allocator&lt;wchar_t&gt; &gt; &amp; _Str, const wchar_t _Delim) line 414 C++\r\nstd::getline&lt;wchar_t,std::char_traits&lt;wchar_t&gt;,std::allocator&lt;wchar_t&gt; &gt;(std::basic_istream&lt;wchar_t,std::char_traits&lt;wchar_t&gt; &gt; &amp; _Istr, std::basic_string&lt;wchar_t,std::char_traits&lt;wchar_t&gt;,std::allocator&lt;wchar_t&gt; &gt; &amp; _Str) line 485 C++<\/pre>\n<p>\u53d1\u73b0\u4e86\u8f6c\u6362\u7684\u4ee3\u7801\u3002\u00a0\u957f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre>_Pcvt-&gt;in(_State,\r\n&amp;*_Str.begin(), &amp;*_Str.begin() + _Str.size(), _Src,\r\n&amp;_Ch, &amp;_Ch + 1, _Dest)<\/pre>\n<p>\u53c2\u6570\u7565\u96be\u61c2\u3002\u867d\u7136\u8f93\u5165\u8f93\u51fa\u7f13\u51b2\u533a\u7684\u9996\u4f4d\u6307\u9488\u662f\u5f88\u5bb9\u6613\u61c2\u5566\uff0c\u90a3\u5176\u4ed6\u52a8\u4e1c\u897f\u5462\u2026\u2026<\/p>\n<p>\u679c\u7136\u8fd8\u662f\u4e0a\u7f51\u53bb\u641c\uff0c\u641c\u51fa\u6765\u5b83\u662f\u8fd9\u6837\u8bf4\u7684\uff0c<\/p>\n<p>_State\u662f\u4fdd\u5b58\u8f6c\u6362\u7684\u72b6\u6001\u3002\u8fd9\u4e2ain\u51fd\u6570\uff0c\u5b83\u662fconst\u7684\uff0c\u8fd9\u6837\u7684\u8bdd\u5c31\u6ca1\u529e\u6cd5\u8bb0\u5f55\u8f6c\u6362\u7684\u72b6\u6001\u4e86\u3002\u4e8e\u662f\u5b83\u7528\u4e86\u8fd9\u4e2a_State\u6765\u4fdd\u5b58\u8f6c\u6362\u7684\u72b6\u6001\u3002\u72b6\u6001\u662f\u5565\uff1f\u6bd4\u5982\u4e00\u4e2aShiftJIS\u7684\u8f6c\u6362\u5668\uff0c\u4f60\u9001\u6c49\u5b57\u7684\u7b2c\u4e00\u4e2a\u5b57\u8282\u8fdb\u53bb\uff0c\u00a0\u5b83\u81ea\u7136\u8f6c\u4e0d\u51fa\u6765\u4ec0\u4e48\u4e1c\u897f\u3002\u90a3\u5b83\u5c31\u4fdd\u5b58\u4e0b\u6765\uff0c\u7136\u540e\u7b49\u7b2c\u4e8c\u4e2a\u5b57\u8282\u3002\u8fd9\u4e2a_State\u5c31\u662f\u7ed9\u5b83\u4fdd\u5b58\u72b6\u6001\u7684\u3002<\/p>\n<p>\u518d\u7136\u540e\uff0c_Src\u548c_Dest\uff0c\u5176\u5b9e\u662f\u4fdd\u5b58\u4e86\u8f6c\u6362\u7684\u8fdb\u5ea6\u3002\u6b63\u5e38\u60c5\u51b5\u4e0b\u5b83\u662f\u4f1a\u5168\u90e8\u8f6c\u5b8c\uff0c\u4f46\u662f\u5982\u679c\u8f6c\u4e00\u534a\u5b83\u9047\u4e0a\u4e86\u4ec0\u4e48\u9519\u8bef\uff0c\u5c31\u4f1a\u505c\u4e0b\u6765\u3002\u8fd9\u4e2a\u65f6\u5019_Src\u548c_Dest\u5c31\u80fd\u770b\u5230\u5b83\u8f6c\u5230\u54ea\u91cc\u4e86\u3002<\/p>\n<p>\u7136\u540e\u4e8e\u6b64\u5bf9\u5e94\u7684\u4e5f\u6709\u4e00\u4e2aout\u51fd\u6570\u3002\u529f\u80fd\u6b63\u597d\u76f8\u53cd\uff08\u4e00\u4e2a\u662f\u8f6c\u8fc7\u53bb\uff0c\u4e00\u4e2a\u662f\u8f6c\u56de\u6765\uff09\u3002<\/p>\n<p>\u597d\uff0c\u90a3\u73b0\u5728\u5c31\u662f\u90a3\u4e2a_Pcvt\uff0c\u6211\u8981\u600e\u4e48\u5f97\u5230\u3002\u56e0\u4e3a\u5b83\u4f3c\u4e4e\u6784\u9020\u51fd\u6570\u8fd8\u662f\u6790\u6784\u51fd\u6570\u7684\u662f\u4e0d\u7ed9\u7528\u6237\u7528\u7684\uff0c\u6b63\u5e38\u65b9\u6cd5\u6765\u6784\u9020\u80af\u5b9a\u4e0d\u884c\uff0c\u5b83\u5e94\u8be5\u6709\u7559\u4ec0\u4e48\u8def\u5427\u3002\u4e0a\u7f51\u641c\u641c\uff0c\u53d1\u73b0\u6807\u51c6\u5e93\u91cc\u9762\u6709\u8fd9\u4e48\u4e00\u4e2a\u51fd\u6570\u7ed9\u4f60\u53d6\u5f97\u8fd9\u4e2a\u5bf9\u8c61\u7684\uff1a<\/p>\n<pre>const std::codecvt&lt;wchar_t, char, int&gt;&amp; std::use_facet&lt; std::codecvt&lt;wchar_t, char, int&gt; &gt;(const std::locale&amp;)<\/pre>\n<p>\u5bf9\u8c61\u62ff\u5230\u4ee5\u540e\uff0c\u5c31\u53ef\u4ee5\u81ea\u5df1\u8bd5\u9a8c\u4e00\u4e0b\u90a3\u79cd\u505a\u6cd5\u4e86\uff1a<\/p>\n<pre>#include &lt;iostream&gt;\r\n#include &lt;locale&gt;\r\n#include &lt;vector&gt;\r\n#include &lt;string&gt;\r\n\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n    locale jpLoc(\"japanese\");\r\n    typedef std::codecvt&lt;wchar_t, char, std::mbstate_t&gt; MyCodeCvt;\r\n    const MyCodeCvt&amp; cvt = std::use_facet&lt;MyCodeCvt&gt;(jpLoc);\r\n\r\n    std::wstring wt(L\"\u65e5\u672c\u8a9e\");\r\n    std::vector&lt;char&gt; buf(wt.length() * 2 + 1);\r\n    const wchar_t* inpProgress;\r\n    char* outProgress;\r\n    mbstate_t state = 0;\r\n\r\n    cvt.out(state,\r\n        &amp;wt[0], &amp;wt[0] + wt.length(), inpProgress,\r\n        &amp;buf[0], &amp;buf[0] + buf.size(), outProgress);\r\n    if (inpProgress != &amp;wt[0] + wt.length())\r\n        std::cerr &lt;&lt; \"convert failed!\" &lt;&lt; std::endl;\r\n    else {\r\n        *outProgress = 0;\r\n\r\n        std::string jpStr(&amp;buf[0]);\r\n        std::cout &lt;&lt; jpStr &lt;&lt; std::endl;\r\n    }\r\n    return 0;\r\n}<\/pre>\n<p>\u3046\u3075\u3075\u2665<\/p>\n<p>\u8865\u5145\uff1a<\/p>\n<p>1\u3001\u5982\u679c\u7528locale jpLoc(\"japanese\")\u6784\u9020\u96d5\u50cf\u7684\u8bdd\uff0c\u518dfs.imbue(jpLoc)\u585e\u5230wfstream\u91cc\u9762\uff0c\u8fd9\u6837\u8f93\u51fa\u6570\u5b57\u7684\u65f6\u5019\u5b83\u4e5f\u4f1a\u7ed9\u4f60\u7528\u7279\u6b8a\u5199\u6cd5\uff0c\u6bd4\u5982fs &lt;&lt; 1000\u5c31\u7ed9\u4f60\"1,000\"\u4e86\u3002\u53ea\u8981\u5b83\u7ba1\u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362\u800c\u4e0d\u7ba1\u5176\u4ed6\u7684\uff0c\u7528locale jpLoc(\"japanese\", std::locale::ctype)\u6765\u6784\u9020\u3002ctype\u6307\u7684\u662f\u53ea\u7ba1\u5b57\u7b26<\/p>\n<p>2\u3001C++\u7684\u65b0\u6807\u51c6\u91cc\u9762\u4f3c\u4e4e\u6709\u4e2astd::wstring_convert\u6765\u7ed9\u4f60\u5e72\u8fd9\u4e8b\u3002\u66f4\u65b9\u4fbf<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u505a\u4e86\u4e2aJubeat Analyzer\u7684\u8c31\u9762\u8f6c\u6210Yubiosi\u8c31\u9762\u7684\u5de5\u5177\u3002\u5728\u5236\u4f5c\u5de5\u5177\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9047\u5230\u4e86\u4e00\u4e2a\u95ee\u9898 &hellip; <a href=\"https:\/\/blog.sorayuki.net\/?p=185\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u5728C++\u4e2d\u8fdb\u884c\u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,6],"tags":[],"class_list":["post-185","post","type-post","status-publish","format-standard","hentry","category-uncategorized","category-6"],"_links":{"self":[{"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=\/wp\/v2\/posts\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=185"}],"version-history":[{"count":0,"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=\/wp\/v2\/posts\/185\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}