我是一个Visual 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()) 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;
}文件database.txt在程序执行之前是这样的:
0 test执行死刑后,我期待着:
0 test
1 green
ż yellow
3 purple不管我得到了什么:
0 test
1 green很容易看出,字符"ż“是一个问题的原因,但我需要使用它,如何获得正确的输出?
发布于 2016-04-18 17:37:45
在我的系统中,流试图使用L'ż'将wcrtomb转换为字节流,但是转换失败(可能是由于区域设置问题?)。
此时,流将以fs.bad()返回true结束,任何进一步的输出都将被跳过。
https://stackoverflow.com/questions/36698946
复制相似问题