相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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;
      cerr << "some problem";
   return 0;
                @Sorush Rabiee, could you please add std::endl at end of last line: customersFile << ws << ws.length() << std::endl just to ensure that flush has been done
– Dewfy
                Feb 24, 2011 at 12:00
                What are the status flags on customersFile?  Is customersFile imbued with a locale supporting unicode?
– AProgrammer
                Feb 24, 2011 at 12:22
                @AProgrammer: I added Qt-less code. Anything changed.... I'm really confused. if I can't use wofstream with unicode, why it exist? :-(
– sorush-r
                Feb 24, 2011 at 13:16
                as explained here this is 'environment determined locale'. A somewhat more generic solution here
– mzzzzb
                Jul 31, 2014 at 16:52
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章