Re: [問題] unicode fopen
看板C_and_CPP (C/C++)作者Hubert (Eyes can tell)時間15年前 (2010/10/12 23:00)推噓1(1推 0噓 1→)留言2則, 2人參與討論串3/3 (看更多)
※ 引述《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
→
10/12 23:19, , 1F
10/12 23:19, 1F
推
10/15 00:55, , 2F
10/15 00:55, 2F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章