我在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;
}发布于 2011-02-24 21:19:19
添加
#include <locale>在main的开头,
std::locale::global(std::locale(""));https://stackoverflow.com/questions/5104329
复制相似问题