首页 > 解决方案 > 在c ++中以阿拉伯名称创建文件

问题描述

我想在 C++ 中创建具有阿拉伯名称的文件。下面是我尝试过的程序。

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

int main() {

    const char *path="D:\\user\\c++\\files\\فثسف.txt";
    std::ofstream file(path); //open in constructor
    std::string data("Hello World");
    file << data;

    return 0;
}

但是文件是用垃圾字符创建的:ÙثسÙ.txt。我正在使用 Windows 平台和 g++ 编译器。

标签: c++g++

解决方案


-fexec-charset可以使用gcc / g++ 的编译器选项指定字符串文字的默认编码。

在 C++11 及更高版本中,还可以使用字符串的u8uU前缀来指定 UTF8、UTF16 和 UTF32 编码:

const char * utf8literal = u8"This is an unicode UTF8 string! 剝Ц";
const char16_t * utf16literal = u"This is an unicode UTF16 string! 剝Ц";
const char32_t * utf32literal = U"This is an unicode UTF32 string! 剝Ц";

使用上述前缀可能会扰乱一些不期望这些特定类型字符串的函数;一般来说,最好设置编译器选项。

在这篇博文中有一篇关于这个主题的精彩文章:http ://cppwhispers.blogspot.com/2012/11/unicode-and-your-application-3-of-n.html

我希望这有帮助。:)


推荐阅读