TCHAR 数组在反斜杠后追加 w

TCHAR array appending w after backslash

我正在编写一个带有多字节字符的 C++ MFC 应用程序,并且我正在尝试迭代 运行 通过驱动器号来检查 USB 连接。我的这部分代码开始导致我在调试模式下出现一些问题:

for(int i = 0; i < 26; i++){
...
    //Possible device path
    TCHAR drivePath[3] = {_T('A'+i), _T(':'), _T('\')};
...
}

驱动器永远找不到,因为这个数组总是在末尾附加 "w"。

例如,i=0drivePath=A:\w

我的假设是它与 multibyte/unicode 相关,但我假设通过使用 TCHAR_T,它会解决这个问题。

有什么问题吗?

您从未使用空字符终止数组。

TCHAR drivePath[3] = {_T('A'+i), _T(':'), _T('\')};

应该是

TCHAR drivePath[4] = {_T('A'+i), _T(':'), _T('\'), _T('[=11=]')};
// or
TCHAR drivePath[] = {_T('A'+i), _T(':'), _T('\'), _T('[=11=]')};
//             ^^ let the compiler figure out the size

另一种选择:

TCHAR drivePath[] = { _T("A:\") };
for (char ch = 'A'; ch <= 'Z'; ch++){
    //Possible device path
    drivePath[0] = ch;
}