【问题标题】:Writing persian ( farsi ) by class wfstream in output file通过类 wfstream 在输出文件中写入波斯语(波斯语)
【发布时间】:2016-01-03 20:10:29
【问题描述】:

如何使用 std::wfstream 将波斯语文本(如“خلیج فارس”)写入文件?
我尝试了以下代码,但它不起作用。

#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::wfstream f("D:\\test.txt", std::ios::out);
    std::wstring s1(L"خلیج فارس");
    f << s1.c_str();
    f.close();

    return 0;
}

运行程序后文件为空。

【问题讨论】:

  • 我检查了输出文件,它是空的
  • f.imbue(std::locale("")); 如果波斯语是您的默认系统区域设置 (stackoverflow.com/a/2169088/1566267)
  • 必须是 std::wfstream 还是也允许 std::fstream?
  • @robsn:是的,正如在ios_base 中声明的那样。
  • 指定您的平台 - Windows、Linux、Mac?在某些操作系统上,控制台是 Unicode 友好的,在某些操作系统上 - 不是那么多。

标签: c++ string unicode persian farsi


【解决方案1】:

您可以使用 C++11 utf-8 字符串文字和标准 fstream 和字符串:

#include <iostream>
#include <fstream>

int main()
{
    std::fstream f("D:\\test.txt", std::ios::out);
    std::string s1(u8"خلیج فارس");
    f << s1;
    f.close();

    return 0;
}

【讨论】:

    【解决方案2】:

    首先你可以离开

    f << s1.c_str();
    

    随便用

    f << s1;
    

    要使用 std::wstream 编写“خلیج فارس”,您必须为波斯语语言环境指定 imbue,例如:

    f.imbue(std::locale("fa_IR"));
    

    在写入文件之前。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多