gpt4 book ai didi

c++ - 无法将 std::wstring 写入 wofstream

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:05:22 27 4
gpt4 key购买 nike

我在 Linux 系统上使用 Qt/C++。我需要转换 QLineEdit的文本到 std::wstring并将其写入 std::wofstream .它适用于 ascii 字符串,但当我输入任何其他字符(阿拉伯语或乌兹别克语)时,文件中没有任何内容。 (文件大小为 0 字节)。

这是我的代码:

wofstream customersFile;
customersFile.open("./customers.txt");
std::wstring ws = lne_address_customer->text().toStdWString();
customersFile << ws << ws.length() << std::endl;

John Smith 的输出在行编辑中输入的是 John Smith10 .但对于 unicode 字符串,什么都没有。

首先我认为这是 QString::toStdWString() 的问题,但是customersFile << ws.length();写入所有字符串的正确长度。所以我想我写错了wstring在文件中。 [?]

编辑:

我在eclipse里又写了一遍。并用g++4.5编译。结果是一样的:

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

using namespace std;

int main()
{
cout << "" << endl; // prints
wstring ws = L"سلام"; // this is an Arabic "Hello"
wofstream wf("new.txt");
if (!wf.bad())
wf << ws;
else
cerr << "some problem";
return 0;
}

最佳答案

添加

#include <locale>

在 main 的开头,

std::locale::global(std::locale(""));

关于c++ - 无法将 std::wstring 写入 wofstream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5104329/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com