c – 写入wofstream会产生异常
发布时间:2020-12-16 07:09:22 所属栏目:百科 来源:网络整理
导读:我只是尝试将一些wchar_t *写入文件,但编译程序的命令行输出如下所示.本质上,程序在尝试编写希腊字符串时会挂起. el_GR.UTF-8terminate called after throwing an instance of 'int'Ακυρ?θηκε (core dumped) 源代码如下 #include iostream#include s
我只是尝试将一些wchar_t *写入文件,但编译程序的命令行输出如下所示.本质上,程序在尝试编写希腊字符串时会挂起.
el_GR.UTF-8 terminate called after throwing an instance of 'int' Ακυρ?θηκε (core dumped) 源代码如下 #include <iostream> #include <stdio.h> #include <fstream> #include <wchar.h> using namespace std; int main(int argc,char **argv) { printf("%sn",setlocale(LC_ALL,"")); wofstream f("xxx.txt",ios::out); if(f.is_open()) { try { f.write(L"good",4); f.flush(); f.write(L"καλημερα",8); f.close(); } catch (int e) { cout << "An exception occurred. Exception Nr. " << e <<endl; } printf("hello worldn"); return 0; } } 为什么? 解决方法
流不从环境中获取区域设置.
我加入了: #include <locale> locale loc("el_GR.utf8"); f.imbue(loc); 所以流现在拥有要使用的语言环境(如果我错了请更正). 正常工作的代码是: #include <iostream> #include <stdio.h> #include <fstream> #include <wchar.h> #include <locale> using namespace std; int main(int argc,char **argv) { locale loc("el_GR.utf8"); wcout<<"Hi I am starting Ξεκιν?με"<<endl; cout<<"%sn"<<setlocale(LC_ALL,"en_US.utf8")<<endl; wofstream f("xxx.txt",ios::out); if(f.is_open()){ f.imbue(loc); f.write(L"good",4);f.flush(); f.write(L"καλημ?ρα",8); f.close(); cout<<"fileClosed"<<endl; } cout<<"hello world"<<endl; return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |