无法将std :: wstring写入wofstream [英] Unable to write a std::wstring into wofstream

查看:158
本文介绍了无法将std :: wstring写入wofstream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux系统上使用Qt/C ++.我需要将QLineEdit的文本转换为std::wstring并将其写入std::wofstream.它对于ascii字符串正确工作,但是当我输入任何其他字符(阿拉伯语或乌兹别克语)时,文件中没有任何内容. (文件大小为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).

这是我的代码:

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字符串,什么也没有.

Output for John Smith entered in the line edit is John Smith10. but for unicode strings, nothing.

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

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. [?]

我再次在日食中写下来.并使用g ++ 4.5进行了编译.结果是一样的:

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;
}

推荐答案

添加

#include <locale>

在main的开头

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

这篇关于无法将std :: wstring写入wofstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆