2

I can see there are many questions related to strings and wide strings. But as none of them gives me information I am looking for... I am posting a new question.

I have this code...

std::string myName("vikrant");
std::cout<<myName<<std::endl;

std::wstring myNameHindi = L"मुरुगन";
std::wcout<<myNameHindi<<"-----"<<myNameHindi.size()<<std::endl;
std::wcout<<L"मुरुगन"<<std::endl;

std::string myNameHindiS = "मुरुगन";
std::cout<<myNameHindiS<<"-----"<<myNameHindiS.size()<<std::endl;

when I compile & run this code on my RHEL box(... (connected through ssh, running gcc 4.1.2) I get this o/p (please note middle two lines are not printing properly)

vikrant
.A0A(-----6
.A0A(
मुरुगन-----18

While on my apple laptop and one of FreeBSD(through ssh) box I dont get o/p from w_* code. I just get first and last cout executed vikrant मुरुगन-----18

My understanding was that if not specified these strings will be treated as UTF 8. and if string can handle it wstring will handle as well. Is there something wrong in that approach? Some addon questions are... is it just a display problem? or wstring is not reliable on linux? Any additional information may help as well.

vikrant
  • 393
  • 4
  • 15
  • Try looking here: http://stackoverflow.com/questions/402283/stdwstring-vs-stdstring – in need of help Mar 18 '14 at 09:04
  • I went through this question... it was really very good read, but my problem is more specific of o/p I am getting. wstring not printing string on one platforms, and printing nothing on other. I need some information on how compiler-editor-OS affect this processing. – vikrant Mar 18 '14 at 09:24
  • possible duplicate of [Mixing cout and wcout in same program](http://stackoverflow.com/questions/8947949/mixing-cout-and-wcout-in-same-program) – n. m. could be an AI Mar 18 '14 at 09:37
  • @n.m. I infact went through that question... it says(i.e. most popular answer) that "once the orientation of a stream is set, you should not call a function which is not compatible with the orientation of that stream" But when I tried that.. i.e. started my program with a wcout (inplace of cout)... it printed just two lines with question marks.. i.e. .. ??????-----6 ?????? It was not printing std::cout. I understood that mixing these two is causing this issue... but could not understand why I am not getting readable o/p (from wcout) in either of the case. – vikrant Mar 18 '14 at 11:54
  • If you are not getting readable output from wcout without mixing stream orientation, ask a question about *that*. – n. m. could be an AI Mar 18 '14 at 13:57

1 Answers1

2

EASIEST WAY

Here is what are you looking for, #include <clocale> and for example, to have Turkish, just simply type setlocale(LC_ALL,"Turkish"); to your code.

You can also just leave it as setlocale(LC_ALL,""); it will use your local language.

#include <iostream>
#include <clocale>

int main(){

setlocale(LC_ALL,"Turkish");

std::cout << "I can type any Turkish character like ÖöÇ窺İiĞğÜüİ, anything.\n" << std::endl;

system("pause");
return 0;
}

SOME OTHER WEIRD WAY TO DO IT

This is a really weird way to do it but it will also work.

#include <iostream>
int main()
{
std::string characters="IiĞğÇçÜüŞşÖö";
int i;
    for ( i=0; i<characters.length(); ++i ){
         characters[i]=(characters[i]==-2) ? 159:characters[i]; //ş
         characters[i]=(characters[i]==-3) ? 141:characters[i]; //ı
         characters[i]=(characters[i]==-4) ? 129:characters[i]; //ü
         characters[i]=(characters[i]==-10) ? 148:characters[i]; //ö
         characters[i]=(characters[i]==-16) ? 167:characters[i]; //ğ
         characters[i]=(characters[i]==-25) ? 135:characters[i]; //ç
         characters[i]=(characters[i]==-34) ? 158:characters[i]; //Ş
         characters[i]=(characters[i]==-35) ? 152:characters[i]; //İ
         characters[i]=(characters[i]==-36) ? 154:characters[i]; //Ü
         characters[i]=(characters[i]==-42) ? 153:characters[i]; //Ö
         characters[i]=(characters[i]==-48) ? 166:characters[i]; //Ğ
         characters[i]=(characters[i]==-57) ? 128:characters[i]; //Ç

         std::cout << characters[i] << " ";
    }
}
  • thanks for your answers.. but I am looking for more specific answers... which can tell me what I need to understand to get a grip on printing these string on console. – vikrant Mar 18 '14 at 09:36