无法将 std::wstring 写入 wofstream
我在 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;
}
I'm using Qt/C++ on a Linux system. I need to convert a QLineEdit
's text to std::wstring
and write it into a std::wofstream
. It works correctly for ascii strings, but when I enter any other character (Arabic or Uzbek) there is nothing written in the file. (size of file is 0 bytes).
this is my code:
wofstream customersFile;
customersFile.open("./customers.txt");
std::wstring ws = lne_address_customer->text().toStdWString();
customersFile << ws << ws.length() << std::endl;
Output for John Smith
entered in the line edit is John Smith10
. but for unicode strings, nothing.
First I thought that is a problem with QString::toStdWString()
, but customersFile << ws.length();
writes correct length of all strings. So I guess I'm doing something wrong wrong with writing wstring
in file. [?]
EDIT:
I write it again in eclipse. and compiled it with g++4.5. result is same:
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加and ,
在 main 的开头
Add
and at the start of main,