Adding a TCHAR to a wstring: doesn't work
I have something pretty simple to do, I am trying to prompt the user for character input & save that character onto a string. Then I print that whole string.
The program is for windows but I want the program to work for both ASCII & Unicode which is why I user TCHAR, & wstring.
My Problem: I am unable to add the character(that the user inputs) onto a wstring. It just wont be stored in that variable. Why?
My simple code:
#include <windows.h>
#include <tchar.h>
#include <conio.h>
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int main()
{
// I am using wstring for unicode compatibility but in Windows(MSDN) is there a general
// string variable? You know how there is char, wchar & then TCHAR(which is a general variable
// for both ASCII & unicode). Is there a TSTRING or something?
wstring chStr = L"abc";
TCHAR ch = L'a';
while ( ch != '!' )
{
printf("Enter a开发者_如何学Go character: ");
ch = getch();
chStr += ch; // the string never takes on a char it always remains as "a"
printf("\nThe characters entered so far are: %s \n", chStr.c_str());
}
system("PAUSE");
return 0;
}
You can use tchar* input
and then
wstring chStr = L"abc";
std::wstring s(input)
chStr += s;
http://linux.about.com/library/cmd/blcmdl3_wprintf.htm
says you need to use %ls not %s with wprintf()
:)
you see, it's reading the wide string as a char string with one char and a null.
Your test isn't showing what you think it is showing.
The printf
function expects ASCII string parameters for the %s
format specification. The UNICODE representation for the letter a
is 0x0061 which is stored in memory as 0x61, 0x00 (because we're dealing with a little-endian system). Since printf is interpreting the memory as an ASCII string, 0x61, 0x00 looks to be a null terminated string that is one character long, so that's what you get printed.
It doesn't really make sense to use TCHAR anymore. The TCHAR type is either WCHAR or char depending on whether the macro UNICODE is defined or not. TCHAR is useful in cases where you want to write code that can be compiled twice - once for ASCII and once for UNICODE. For example when you wanted to write code that could be compiled to run efficiently on ASCII platforms (like Windows 95) and compiled again to run efficiently on UNICODE platforms (like Windows XP).
Now that all current Windows operating systems are natively UNICODE, there's not much use for TCHAR and some risk to using it.
For example, your code TCHAR ch = L'a';
is valid when compiling for UNICODE because TCHAR is defined to be WCHAR in that case. But, when compiling for non UNICODE, TCHAR is defined to be char and assigning a Unicode character like L'a'
to a char variable doesn't really make sense. You won't necessarily get a syntax error, but you won't necessarily get the code you expect either.
Note Where I used "ASCII" above I probably should really have said "multi-byte character set" or "non-UNICODE character set" since not all non-UNICODE character sets are ASCII.
精彩评论