在c+中使用fstream读取和写入UTF16文件+; 我不熟悉C++流的东西。所以我练习流输入,流输出。 当然,fstream也是

在c+中使用fstream读取和写入UTF16文件+; 我不熟悉C++流的东西。所以我练习流输入,流输出。 当然,fstream也是,c++,fstream,C++,Fstream,读写基于多字节字符编码的文件不是问题,但是 我被utf16这样的宽字符卡住了 这是我的代码和测试文件 vector<wstring> strings; wifstream ifs(L"inputtxt/utf16.txt", std::wifstream::binary | std::wifstream::in); wchar_t buff[1024] = {0,}; while (!ifs.fail()){ ifs.getline(buff, 1024); wstring

读写基于多字节字符编码的文件不是问题,但是 我被utf16这样的宽字符卡住了

这是我的代码和测试文件

vector<wstring> strings;
wifstream ifs(L"inputtxt/utf16.txt", std::wifstream::binary | std::wifstream::in);
wchar_t buff[1024] = {0,};
while (!ifs.fail()){
  ifs.getline(buff, 1024);
  wstring str(buff);
  strings.push_back(str);
}
ifs.close();

wofstream ofs(L"outputtxt/utf16.txt", std::wofstream::out | std::wifstream::binary);
vector<wstring>::iterator it = strings.begin();
for (; it != strings.end(); it++ ) {
  ofs.write(it->c_str(), it->length());
  ofs.put(L'\n');
}
ofs.close();
有什么问题吗?我如何才能正确地做到这一点


请给我小费。谢谢。

有什么问题吗?
-->您的问题中缺少这一重要事实。如果你不说什么不起作用,我们就帮不了你。注意:
wchar\u t
是一个奇怪的怪兽,它的位宽度取决于你使用的编译器/平台;在C++11中,对于UTF-16,最好使用显式的
char16\t
,至少您会知道它正好是16位。
what are you doing? 
what are you doing?