By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,918 Members | 1,557 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,918 IT Pros & Developers. It's quick & easy.

convert char to wchar

P: 11
This is a test code to convert chat to wchar the problem is that each time the else statement is executed .....So the conversion is not working !!!!!!!

Expand|Select|Wrap|Line Numbers
  1. char p[500];
  2.  while(getline(inFile1, line))
  3.   {     
  4.     //we read p from file that contain only one word "why"............for testing
  5.     strcpy_s(p, 500, line.c_str());
  7.     string search = p;
  8.     const size_t newsize = 100;
  9.     size_t origsize = strlen(search.c_str()) + 1;
  10.     size_t convertedChars = 0;
  11.     wchar_t wcstring[newsize];
  12.     mbstowcs( wcstring, search.c_str(), newsize);
  13.     wcscat_s(wcstring, L" (wchar_t *)");
  15.     if(wcstring == L"why")
  16.             cout<<"Right conversion";
  17.             else
  18.             cout<<"Wrong conversion";
Oct 1 '10 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 9,197
Expand|Select|Wrap|Line Numbers
  1. if(wcstring == L"why")
This is always false.

wcstring is an array of wchar_t. Therefore, the name wcstring is the address of element 0. Ditto for L"why". That's the address of the "w".

Since there are two arrays the addresses will always be different.

I suggest you use wcscmp, or if you use the == operator, do so with two wstring objects.
Oct 3 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.