- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的代码:
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <tchar.h>
#define MAX_KEY_LENGTH 6
void CheckForUIMatch(TCHAR *ui, TCHAR *UIToFind, TCHAR *UIFound);
int _tmain(int argc, LPTSTR argv[])
{
TCHAR UIToFind[24] = _T("hi***;en-US; ");
TCHAR UIFound[24] = _T("");
LPCTSTR placeToLook = _T("SYSTEM\\CurrentControlSet\\Control\\MUI\\UILanguages");
HKEY UIKey;
DWORD subkeyCounter = 0;
TCHAR subkeyName[MAX_KEY_LENGTH];
DWORD subkeyLength;
DWORD subkeyAmount = 0;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, placeToLook , 0, KEY_READ, &UIKey) == ERROR_SUCCESS) {
RegQueryInfoKey(UIKey, NULL, NULL, NULL, &subkeyAmount, NULL, NULL, NULL, NULL, NULL, NULL, NULL); // Get the length of the longest subkey.
for (subkeyCounter = 0; subkeyCounter < subkeyAmount; subkeyCounter++) {
subkeyLength = MAX_KEY_LENGTH;
RegEnumKeyEx(UIKey,
subkeyCounter,
subkeyName,
&subkeyLength,
NULL,
NULL,
NULL,
NULL);
CheckForUIMatch(subkeyName, UIToFind, UIFound);
}
} else {
_tprintf(L"Faild to open the registry key!");
}
_tprintf(_T("Match: %s"), UIFound);
return 0;
}
void CheckForUIMatch(TCHAR *ui, TCHAR *UIToFind, TCHAR *UIFound)
{
int charToMatch;
TCHAR* token = _tcstok(UIToFind, L";");
while(token != NULL) {
/* Check for exact match or general match */
if ((token[_tcslen(token)-2] == '*') && (token[_tcslen(token)-1] == '*')) { // make general lang match
charToMatch = 2;
} else {
charToMatch = 5; // make exact lang match
}
_tprintf(L"\ntoken: %s, to match %d.\n", token, charToMatch); //Debug
if(!_tcsncmp(token, ui, charToMatch)) {
_tcsncat(UIFound, ui, charToMatch);
_tcscat(UIFound, _T(";"));
}
token = _tcstok(NULL, L";");
}
}
问题:
CheckForUIMatch 处的 while 循环似乎仅执行一次工作,但在第二次运行循环时(例如,当在注册表中找到多个 UI 时),它仅在一个上循环 token (例如 hi***)而不是第二个,一旦它结束循环并且需要获取第二个 token ,我可以在调试器中看到它获取 BatPointer 而不是第二个 token 。
如果我将 UIToFind TCHAR 直接放入 CheckForUIMatch 函数中,一切正常,我不知道为什么?有人知道吗?
最佳答案
strtok
就地修改搜索字符串以分隔子字符串。当您调用 strtok
时,您的搜索字符串会随之演变:
"hi***;en-US; "
"hi***\0en-US; "
"hi***\0en-US\0 "
当您尝试在第二轮调用 strtok
中重用此字符串时,该函数会看到 "hi***"
因为第一个 '\0'
终止字符串。
不要每次都分割字符串,而是分割一次或将其声明为数组,例如:
LPCTSTR UiToFind[] = {_T("hi***"), _T("en-US") };
关于c - _tcstok (strtok) 跳转到 token 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11305116/
我正在尝试使用 _tcstok 标记文件中的行。我能够对该行进行标记化一次,但是当我第二次尝试对其进行标记化时,我遇到了访问冲突。我觉得这与实际访问的值无关,而是与访问位置有关。不过,我不确定还有什么
这是我的代码: #include #include #include #include #define MAX_KEY_LENGTH 6 void CheckForUIMatch(TCHAR
我正在尝试运行以下代码 TCHAR* str1 = TEXT("C:\\Program Files\\Internet Explorer;"); const TCHAR* del = TEXT
我是一名优秀的程序员,十分优秀!