Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何输出/输入多字节符号?_C++_Unicode_Utf 16_Stdstring - Fatal编程技术网

C++ 如何输出/输入多字节符号?

C++ 如何输出/输入多字节符号?,c++,unicode,utf-16,stdstring,C++,Unicode,Utf 16,Stdstring,经过大量搜索后,我开始使用奇怪的代码: ofstream myfile; string chars = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя"; myfile.open ("alphabet.txt"); for (int i = 0; i < 66; i+=2) { myfile << chars[i] <<chars[i+1] << &q

经过大量搜索后,我开始使用奇怪的代码:

ofstream myfile;
    string chars =  "абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
    myfile.open ("alphabet.txt");
    for (int i = 0; i < 66; i+=2) {
        myfile << chars[i] <<chars[i+1] << "\n";
    }
    myfile.close();
流myfile的
;
字符串字符;
myfile.open(“alphabet.txt”);
对于(int i=0;i<66;i+=2){

myfile这在我的机器上运行。我的源代码文件是UTF-8。字符串是UTF-16。输出是UTF-16LE

随着时间的推移,C++在处理Unicode字符串方面有所改进,但仍有很大的改进空间

#include <fstream>
#include <string>

using std::ofstream;
using std::string;

int main() {
    auto chars = u"абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
    auto myfile = ofstream("alphabet.txt");
    for (char16_t const* p = chars; *p; ++p) {
        auto c = *p;
        auto cc = reinterpret_cast<char const*>(&c);
        myfile.write(cc, sizeof c);
    }
    myfile.close();
}
#包括
#包括
使用std::of流;
使用std::string;
int main(){
自动字符=u“ббгжзззззбзбзззззббгбг;
auto myfile=ofstream(“alphabet.txt”);
对于(char16_t const*p=chars;*p;++p){
自动c=*p;
自动抄送=重新解释铸件(&c);
write(cc,sizeof c);
}
myfile.close();
}

<代码> STD::String 包含代码> char < /Case> s,它总是包含“代码> char < /Case> s。s使用非拉丁字符,一种选择是使用区域编码,如Koi8r,但是使用<代码> KII8R/<代码>和其他字符集已被时间限制,而现代C++实现。tations通常默认为多字节UTF-8以表示非拉丁字符,其中每个字符,如“б”它是由多字节序列表示的。我知道它,但是为什么没有内置的方法来获取这样的字母?!C++库确实有一些类用于在各种编码和Unicode之间进行代码转换,但是它们很难使用,而且不太受欢迎。这样做,比如代码> iCave
。@ DmitrySokolov,我猜是因为C++早于Unicode的广泛采用。