首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >写入wofstream会产生异常

写入wofstream会产生异常
EN

Stack Overflow用户
提问于 2014-07-25 02:25:51
回答 1查看 215关注 0票数 4

我尝试将一些wchar_t*写到一个文件中,但是编译后的程序的命令行输出如下所示。本质上,程序在尝试写入希腊字符串时挂起。

代码语言:javascript
复制
el_GR.UTF-8
terminate called after throwing an instance of 'int'
Ακυρώθηκε (core dumped)

源代码如下

代码语言:javascript
复制
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <wchar.h>
using namespace std;

int main(int argc, char **argv)
{

    printf("%s\n",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 world\n");
        return 0;
    }
}

为什么?

EN

回答 1

Stack Overflow用户

发布于 2014-07-25 04:32:13

流不会从环境中获取区域设置。我补充说:

代码语言:javascript
复制
#include <locale>
locale loc("el_GR.utf8");
f.imbue(loc);

因此,流现在保存了要使用的语言环境(如果我错了,请纠正)。

正常工作的代码是:

代码语言:javascript
复制
#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<<"%s\n"<<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;


}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24941299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档