作者Hubert (Eyes can tell)
看板C_and_CPP
標題Re: [問題] unicode fopen
時間Tue Oct 12 23:00:21 2010
※ 引述《deepking (wnuiayldh)》之銘言:
: 目前只找到先讀進來再用mbstowcs將char轉成wchar_t
: 在linux上,
: 有沒有辦法直接讀進來就是wchar_t ?
#include <iostream>
#include <fstream>
#include <string>
#include <locale>
#include <iterator>
#include <boost/archive/detail/utf8_codecvt_facet.hpp>
int main(int argc, char *argv[])
{
std::wifstream wfin("utf8.txt", std::wios::binary);
std::locale old_locale;
std::locale utf8_locale(old_locale,
new boost::archive::detail::utf8_codecvt_facet);
wfin.imbue(utf8_locale);
wfin.unsetf(std::wios::skipws);
std::wios::sync_with_stdio(false);
std::wstring data;
std::copy(std::istream_iterator<wchar_t, wchar_t>(wfin),
std::istream_iterator<wchar_t, wchar_t>(),
std::back_inserter(data)
);
std::wcout << data;
wfin.close();
return 0;
}
你可以透過 locale 跟 facet 去做...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.116.188.6
→ deepking:thx~真方便@@ 10/12 23:19
推 tomap41017:w太誇張了XDDDDDDDD 10/15 00:55