Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.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_G++ - Fatal编程技术网

C++ 流不写

C++ 流不写,c++,unicode,g++,C++,Unicode,G++,我用C++编写了以下代码: #include <iostream> #include <fstream> #include <string> using namespace std; int main(){ wstring ws1 = L"Infinity: \u2210"; wstring ws2 = L"Euro: €"; wchar_t w[] = L"Sterling Pound: £"; wfstream ou

我用C++编写了以下代码:

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

using namespace std;

int main(){
    wstring ws1 = L"Infinity: \u2210";
    wstring ws2 = L"Euro: €";

    wchar_t w[] = L"Sterling Pound: £";

    wfstream out("/tmp/unicode.txt");
    out.write(ws1.c_str(), ws1.size());
    out << ws1 << endl << ws2 << endl << w << endl;
    out.flush();
    out.close();
}
#包括
#包括
#包括
使用名称空间std;
int main(){
wstring ws1=L“无穷大:\u2210”;
wstring ws2=L“欧元:欧元”;
wchar_t w[]=L“英镑:镑”;
wfstream out(“/tmp/unicode.txt”);
write(ws1.c_str(),ws1.size());

out始终首先设置区域设置…执行
locale::global(locale(“”);
。在此之前,您处于纯C模式,对UTF-8一无所知

在Darwin上,这是不正确的,所以我需要执行
setlocale(LC_ALL,“”);
,但是您的程序可以为我工作

编辑 哎呀,你一次就被两个gotcha咬了一口。用默认openmode打开一个
wfstream
不会创建文件。在运行程序之前,我无意中将其修复为
wofstream
,但后来忘记了。很抱歉。因此:

wofstream out("/tmp/unicode.txt");


标准中没有关于字符流转换为字符设备的宽度的定义,您需要对系统进行实验以了解发生了什么。通常,设置本地通过C应该适用于std in/out streams std::cin/std::cout

setlocale("");  // Loads the local that the machine is configured for (see you config)
                // If it is not configured it default to the "C" locale
文件流对象可能不会自动检索本地文件,因此有时需要显式设置流的本地文件

std::locale   defaultLocale(""); // from machine config
std::wfstream out;
out.imbue(defaultLocale);        // imbue must be done before opening
                                 // otherwise it is ignored.

out.open("/tmp/unicode.txt");
让我们做一些测试,以确保您确实在编写:

if (!out)
{
    std::cout << "Failed to open file\n";
}
另一项说明:

out.flush();    // flush() happens automatically when the file is closed
out.close();    // close() happens automatically when the stream is destroyed.
                // So technically it is better not to use these
                // as they are specific to file streams which will prevent you from
                // easily replacing this with a generic stream

这与编译器关系不大,与运行时关系更大。那么您运行的标准运行时libs的版本是什么。@马丁:这是wrt locales的正确行为,运行时版本与编译器版本相同。不给您正确答案是不公平的,两者都是对的,但我决定检查另一个几乎是t的版本二十分钟前,下次
out.write(ws1.c_str(), ws1.size()); // size() is the number of characters (wide)
                                    // write is expecting the number of bytes.
out.flush();    // flush() happens automatically when the file is closed
out.close();    // close() happens automatically when the stream is destroyed.
                // So technically it is better not to use these
                // as they are specific to file streams which will prevent you from
                // easily replacing this with a generic stream