c++ - 在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++ 编译器。
解决方案
-fexec-charset
可以使用gcc / g++ 的编译器选项指定字符串文字的默认编码。
在 C++11 及更高版本中,还可以使用字符串的u8
、u
和U
前缀来指定 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
我希望这有帮助。:)
推荐阅读
- tensorflow - 将两个 tensorflow 数据集连接成一个数据集元素,如示例所示
- c++ - 无法弄清楚如何使用 SDL2 正确格式化 Makefile。未定义参考
- java - 我的代码一直在打印“ [Ljava.lang.String;@4e50df2e”,不太确定该放在哪里以及如何放置 toString()
- java - 需要使用 java 的 lamda 表达式打印字符串的最后一位
- java - 调试:合并排序
- selenium - 使用 goroutine 意外关闭 selenium
- c - C 编程中的蛮力搜索,在 C 编程中读取 .fna 文件
- go - 将 int 切片转换为十六进制值的更好方法
- java - 使用 Spring clound contract 为云合约端点编写生产者测试
- c# - 如何在 C# 中使用 LINQ 应用右外连接?