std::wofstream 运算符<< 未按预期工作
std::wofstream operator<< not working as expected
我是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()) 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
很容易看出,字符"ż"是一个问题的原因,但我需要使用它,如何获得正确的输出?
在我的系统中,流试图使用wcrtomb
将L'ż'
转换为字节流,但转换失败(可能是由于区域设置问题?)。
此时,流以fs.bad()
返回true结束,并且跳过任何进一步的输出。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- 如何防止clang格式在流运算符调用之间添加换行符<<
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- (C 14)操作员&lt;&lt;超负荷无法正如智能指针向量所预期的那样工作
- 如何显示/打印字符串对象?cout & lt; & lt;Int工作,count <<字符串