c++ - 将 TCHAR 添加到 wstring : doesn't work

标签 c++ unicode wstring

我有一些非常简单的事情要做,我正在尝试提示用户输入字符并将该字符保存到字符串中。然后我打印整个字符串。

该程序适用于 Windows,但我希望该程序同时适用于 ASCII 和 Unicode,这就是我使用 TCHAR 和 wstring 的原因。

我的问题:我无法将(用户输入的)字符添加到 wstring 中。它只是不会存储在该变量中。为什么?

我的简单代码:

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

最佳答案

您可以使用tchar* 输入 然后

 wstring chStr = L"abc";  
 std::wstring s(input)
 chStr += s;

关于c++ - 将 TCHAR 添加到 wstring : doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6639476/

相关文章:

c++ - 不要临时调用 QString::operator[]()

C++ protected 成员访问

c++ - 按成员(member)值投

python - 为什么用 universal_newlines 打开子进程会导致 unicode 解码异常?

c++ - std::wstring 导致堆内存分配崩溃

c++ - 从字符中删除重音

c++ - std::wstring VS std::string

c++ - Windows 上使用 C++ 的 google protobuf 时间戳未声明的标识符

c++ - 在 Windows 控制台中显示 unicode 棋子

python - 属性错误: 'unicode' object has no attribute 'remove'