为什么getline正在读取我的整个unicode文件

时间:2013-12-05 08:17:36

标签: c++ string unicode getline

我见过很多线程,但没有一个解决方案对我有用,所以如果有人能投出一些很棒的光 我正在读取unicode文件并使用getline我尝试逐行扫描,然后它扫描整个文件,因为对象是wstring它不允许我在getline中放置分隔符。并且只询问wchar_t,其中我无法适应分隔符。 (\ 0不起作用,因为我在二进制模式下阅读)所以下面是代码片段 平台:Windows,Visual Studio 2010 Unicode编码:UTF 16

wifstream fin("profiles1.prd", ios_base::binary);  //open a file
wofstream fout("DXout.txt",ios_base::binary);  // this dumps the parsing ouput
fin.imbue(std::locale(fin.getloc(),new std::codecvt_utf16<wchar_t, 0x10ffff, std::consume_header>));
fout.imbue(std::locale(fin.getloc(),new std::codecvt_utf16<wchar_t, 0x10ffff, std::consume_header>));
wstring stream;
getline(fin,stream);

1 个答案:

答案 0 :(得分:4)

我希望这是你正在寻找的东西:

fin.imbue(std::locale(fin.getloc(), new std::codecvt_utf16<wchar_t, 0x10ffff,
        std::codecvt_mode(std::little_endian|std::consume_header)>);

Windows是little-endian,因此要跳过BOM imbue utf16,你需要通过发明一种新的转换模式将其打入肠道。

希望它可以帮到你。我将一边留给你。