C++ 标准::wofstream操作员<&书信电报;没有按预期工作
我是Visual Studio 2015用户,试图编写以下函数:C++ 标准::wofstream操作员<&书信电报;没有按预期工作,c++,C++,我是Visual Studio 2015用户,试图编写以下函数: void saveWordsToFile(const std::string& filename, const std::vector<std::pair<std::wstring, std::wstring>>& words) { std::wofstream fs(filename, std::ios::out | std::ios::app); if (fs.fail(
void saveWordsToFile(const std::string& filename, const std::vector<std::pair<std::wstring, std::wstring>>& words)
{
std::wofstream fs(filename, std::ios::out | std::ios::app);
if (fs.fail()) throw std::runtime_error("loadTextFromFile -> Failed to open '" + filename + "'!");
for (auto& word : words) fs << word.first << " " << word.second << std::endl;
fs.close();
}
int main()
{
std::vector<std::pair<std::wstring, std::wstring>> words;
words.push_back({ L"1", L"green" });
words.push_back({ L"ż", L"yellow" });
words.push_back({ L"3", L"purple" });
saveWordsToFile("database.txt", words);
return 0;
}
执行后,我期待:
0 test
1 green
ż yellow
3 purple
然而,我得到:
0 test
1 green
很容易看出字符“ż”是一个问题原因,但是我需要使用它,如何获得正确的输出?在我的系统上,流尝试使用将
L'ż'
转换为字节流,但转换失败(可能是由于区域设置问题?)
在这一点上,流以返回true的
fs.bad()
结束,并跳过任何进一步的输出。?示例输入?示例输出?如果您将std::distance(words.begin(),words.end())
输出到屏幕,您会得到什么?@lightness racesinorbit我可以使用pastebin发布这些示例吗?@Heaven31415否。您的问题正文中应该包含足够的代码来重现问题。@Heaven31415:它们应该足够小,可以一字不差地回答问题,虽然也提供一个到现场演示的链接(ideone.com、coliru等)会很方便。
0 test
1 green